0% found this document useful (0 votes)
225 views

Class Test2

This document contains 30 multiple choice questions related to Java programming concepts such as variables, operators, control flow, and strings. The questions cover topics like switch statements, ternary expressions, if/else statements, logical operators, and string comparisons.

Uploaded by

Jasvan Sundar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views

Class Test2

This document contains 30 multiple choice questions related to Java programming concepts such as variables, operators, control flow, and strings. The questions cover topics like switch statements, ternary expressions, if/else statements, logical operators, and string comparisons.

Uploaded by

Jasvan Sundar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

1.

Which of the following variable types is not permitted in a

switch statement?

1. String

2. double

3. int

4. char

2. What is the value of tip after executing the following code

snippet?

int meal = 5;

int tip = 2;

int total = meal + (meal>6 ? ++tip : ‐‐tip);

1. 1

2. 2

3. 3

4. 6

3. What is the output of the following application?

package registration;

public class NameCheck {

public static void main(String... data) {

String john = "john";

String jon = new String(john);

System.out.print((john==jon)+" "+

(john.equals(jon)));

}
1. true true

2. true false

3. false true

4. false false

4. What is the output of the following application?

package planning;

public class ThePlan {

public static void main(String[] input) {

int plan = 1;

plan = plan++ + --plan;

if(plan==1) {

System.out.print("Plan A");

} else { if(plan==2) System.out.print("Plan B");

} else System.out.print("Plan C");

1. Plan A

2. Plan B

3. Plan C

4. None of the above

5. Which of the following statements about a default branch in a

switch statement is correct?

1. All switch statements must include a default statement.

2. The default statement is required to be placed after all case

statements.

3. Unlike a case statement, the default statement does not take a value.
4. A default statement can only be used when at least one case

statement is present.

6. What is the value of thatNumber after the execution of the

following code snippet?

long thatNumber = 5 >= 5 ? 1+2 : 1*1;

if(++thatNumber < 4)

thatNumber += 1;

1. 3

2. 4

3. 5

4. The answer cannot be determined until runtime.

7. Which statement immediately exits a switch statement, skipping

all remaining case or default branches?

1. exit

2. break

3. goto

4. continue

8. Which statement about ternary expressions is true?

1. In some cases, both expressions to the right of the conditional operator

in a ternary expression will be evaluated at runtime.

2. Ternary expressions require parentheses for proper evaluation.

3. The ternary expressions are a convenient replacement for an if-then-

else statement.
4. Ternary expressions support int and boolean expressions for the left-

most operand.

9. What is the output of the following application?

package voting;

1: public class Election {

2: public void calculateResult(Integer candidateA,

Integer candidateB) {

3: boolean process = candidateA == null ||

candidateA.intValue() < 10;

4: boolean value = candidateA && candidateB;

5: System.out.print(process || value);

6: }

7: public static void main(String[] unused) {

8: new Election().calculateResult(null,203);

9: }

10: }

1. true

2. false

3. The code does not compile.

4. The code compiles but throws a NullPointerException on line 3 at

runtime.

10. What is the output of the following application?

package dinosaur;
public class Park {

public final static void main(String... arguments) {

int pterodactyl = 6;

long triceratops = 3;

if(pterodactyl % 3 >= 1)

triceratops++;

triceratops--;

System.out.print(triceratops);

1. 2

2. 3

3. 4

4. The code does not compile.

11. Which statement about if-then statements is true?

1. An if-then statement is required to have an else statement.

2. If the boolean test of an if-then statement evaluates to false, then the

target clause of the if-then statement will still be evaluated.

3. An if-then statement is required to cast an object.

4. An if-then statement can execute a single statement or a block {}.

12. What is the output of the following application?

package restaurant;

public class Pieces {

public static void main(String[] info) {

int flair = 15;

if(flair >= 15 && flair < 37) {

System.out.print("Not enough");
} if(flair==37) {

System.out.print("Just right");

} else {

System.out.print("Too many");

1. Not enough

2. Just right

3. Too many

4. None of the above

13. Which statement about case statements of a switch statement is

not true?

1. A case value can be final.

2. A case statement must be terminated with a break statement.

3. A case value can be a literal expression.

4. A case value must match the data type of the switch variable, or be

able to be promoted to that type.

14. Given the following truth table, which operator for the boolean

expressions x and y corresponds to this relationship?

x = true x = false

y = true true false

y = false false false

1. --

2. ++

3. ||
4. &&

15. What is the output of the following code snippet?

int hops = 0;

int jumps = 0;

jumps = hops++;

if(jumps)

System.out.print("Jump!");

else

System.out.print("Hop!");

1. Jump!

2. Hop!

3. The code does not compile.

4. The code compiles but throws an exception at runtime.

16. Fill in the blanks: The _____________ operator increases the

value of a variable by 1 and returns the new value, while the

_____________ operator decreases the value of a variable by 1

and returns the original value.

1. pre-increment [++v], pre-decrement [--v]

2. pre-increment [++v], post-decrement [v--]

3. post-increment [v++], pre-decrement [--v]

4. post-increment [v++], post-decrement [v--]

17. What is the output of the following application?

package jungle;

public class TheBigRace {

public static void main(String[] in) {


int tiger = 2;

short lion = 3;

long winner = lion+2*(tiger + lion);

System.out.print(winner);

1. 11

2. 13

3. 25

4. None of the above

18. Given the following code snippet, assuming dayOfWeek is an int,

what variable type of saturday is not permitted?

final _________ saturday = 6;

switch(dayOfWeek) {

default:

System.out.print("Another Weekday");

break;

case saturday:

System.out.print("Weekend!");

1. byte

2. long

3. int

4. None of the above

19. Given the following code snippet, what is the value of dinner
after it is executed?

int time = 11;

int day = 4;

String dinner = time > 10 ? day ? "Takeout" : "Salad" :

"Leftovers";

1. Takeout

2. Salad

3. The code does not compile but would compile if parentheses were

added.

4. None of the above

20. What is the output of the following application?

package recreation;

public class Dancing {

public static void main(String[] vars) {

int leaders = 10 * (2 + (1 + 2 / 5);

int followers = leaders * 2;

System.out.print(leaders + followers < 10 ? "Too

few" : "Too many");

1. Too few

2. Too many

3. The code does not compile.

4. The code compiles but throws a division by zero error at runtime.

21. What is the output of the following application?


package schedule;

public class PrintWeek {

public static final void main(String[] days) {

System.out.print(5 + 6 + "7" + 8 + 9);

1. 56789

2. 11789

3. 11717

4. The code does not compile.

22. Fill in the blanks: The______________ operator is used to find

the difference between two numbers, while the______________

operator is used to find the remainder when one number is

divided by another.

1. /, %

2. –, %

3. %, <

4. –, ||

23. What is the output of the following application?

package transporter;

public class Rematerialize {

public static void main(String[] input) {

int dog = 11;

int cat = 3;

int partA = dog / cat;

int partB = dog % cat;

int newDog = partB + partA * cat;


System.out.print(newDog);

1. 9

2. 11

3. 15

4. The code does not compile.

24. What is the output of the following application?

package dessert;

public class IceCream {

public final static void main(String... args) {

int flavors = 30;

int eaten = 0;

switch(flavors) {

case 30: eaten++;

case 40: eaten+=2;

default: eaten--;

System.out.print(eaten);

1. 1

2. 2

3. 3

4. The code does not compile.

25. What is the output of the following application?

package mode;
public class Transportation {

public static String travel(int distance) {

return distance<1000 ? "train" : 10;

public static void main(String[] answer) {

System.out.print(travel(500));

1. train

2. 10

3. The code does not compile.

4. The code compiles but throws an exception at runtime.

26. Fill in the blanks: Given two non-null String objects with

reference names apples______________ and oranges, if apples

oranges evaluates to true, then apples______________ oranges

must also evaluate to true.

1. ==, equals()

2. !=, equals()

3. equals(), ==

4. equals(), =!

27. For a given non-null String myTestVariable, what is the

resulting value of executing the statement

myTestVariable.equals(null)?

1. true

2. false

3. The statement does not compile.

4. The statement compiles but will produce an exception when used at


runtime.

28. How many 1s are outputted when the following application is

compiled and run?

package city;

public class Road {

public static void main(String... in) {

int intersections = 100;

int streets = 200;

if (intersections < 150) {

System.out.print("1");

} else if (streets && intersections > 1000) {

System.out.print("2");

} if (streets < 500)

System.out.print("1");

else

System.out.print("2");

1. None

2. One

3. Two

4. The code does not compile.

29. Which statement about the logical operators & and && is true?

1. The & and && operators are interchangeable, always producing the

same results at runtime.


2. The & operator always evaluates both operands, while the && operator

may only evaluate the left operand.

3. Both expressions evaluate to true if either operand is true.

4. The & operator always evaluates both operands, while the && operator

may only evaluate the right operand.

30. What is the output of the following code snippet?

int x = 10, y = 5;

boolean w = true, z = false;

x = w ? y++ : y--;

w = !z;

System.out.print((x+y)+" "+(w ? 5 : 10));

1. The code does not compile.

2. 10 10

3. 11 5

4. 12 5

31. What is the output of the following application?

package bob;

public class AreYouBob {

public static void main(String[] unused) {

String bob = new String("bob");

String notBob = bob;

System.out.print((bob==notBob)+" "+

(bob.equals(notBob)));

}
1. true true

2. true false

3. false true

4. false false

32. What is the value of 12 + 6 * 3 % (1 + 1) in Java?

1. 0

2. 12

3. 14

4. None of the above

33. Given the following truth table, the boolean variables p and q,

and the expression p ^ q, what are the missing values in the truth

table, starting with the first column?

p = true p = false

q = true false true

q = false

1. false and true

2. false and false

3. true and true

4. true and false

34. Which of the following is not a possible result of executing the

following application?

public class ConditionallyLogical {

public static void main(String... data) {

if(data.length>=1

&& (data[0].equals("sound") ||
data[0].equals ("logic"))

&& data.length<2) {

System.out.print(data[0]);

1. Nothing is printed.

2. sound is printed.

3. The application throws an exception at runtime.

4. logic is printed.

35. Fill in the blanks: The operators +,______________

,______________ ,______________ , and ++ are listed in the

same or increasing level of operator precedence.

1. *, --, /

2. %, -, *

3. /, *, %

4. *, -, /

36. What statement about the ^ operator is correct?

1. If one of the operands of ^ is true, then the result is always true.

2. There is a conditional form of the operator, denoted as ^^.

3. If both operands of ^ are true, the result is true.

4. The ^ operator can only be applied to boolean values.

37. Given the following Venn diagram and the variables, x, y, and z,

which Java expression most closely represents the filled-in region

of the diagram?

1. x || z

2. y || (y && z)
3. x || y

4. y && x

38. What variable type of red allows the following application to

compile?

package tornado;

public class Kansas {

public static void main(String[] args) {

int colorOfRainbow = 10;

________ red = 5;

switch(colorOfRainbow) {

default:

System.out.print("Home");

break;

case red:

System.out.print("Away");

1. long

2. double

3. int

4. None of the above

39. Which two operators would be used to test if a number is equal to

or greater than 5.21 but strictly less than 8.1?

1. > and <=


2. >= and >

3. < and >=

4. < and >

40. What is the output of the following application?

package transporter;

public class TurtleVsHare {

public static void main(String[] arguments) {

int turtle = 10 * (2 + (3 + 2) / 5);

int hare = turtle < 5 ? 10 : 25;

System.out.print(turtle < hare ? "Hare wins!" :

"Turtle wins!");

1. Hare wins!

2. Turtle wins!

3. The code does not compile.

4. The code compiles but throws a division by zero error at runtime.

41. What is the output of the following application?

public class CountEntries {

public static int getResult(int threshold) {

return threshold > 5 ? 1 : 0;

public static final void main(String[] days) {

System.out.print(getResult(5)+getResult(1)

+getResult(0)+getResult(2)+"");

}
1. 0

2. 1

3. 0000

4. 1000

42. What is the output of the following application?

package yoyo;

public class TestGame {

public String runTest(boolean spinner, boolean

roller) {

if(spinner = roller) return "up";

else return roller ? "down" : "middle";

public static final void main(String pieces[]) {

final TestGame tester = new TestGame();

System.out.println(tester.runTest(false,true));

1. up

2. middle

3. down

4. The code does not compile.

43. Fill in the blanks: The______________ operator is true if either

of the operands are true, while the______________ operator

flips a boolean value.

1. +, -

2. &&, !

3. |, -
4. ||, !

44. Given the following code snippet, what is the value of

movieRating after it is executed?

int characters = 5;

int story = 3;

double movieRating = characters <= 4 ? 3 : story>1 ? 2

: 1;

1. 2.0

2. 3.0

3. The code does not compile but would compile if parentheses were

added.

4. None of the above

45. Fill in the blanks: A switch statement can have______________

case statements and______________ default statements.

1. at most one, at least one

2. any number of, at most one

3. at least one, any number of

4. at least one, at most one

46. Which of the following is not a possible result of executing the

following application?

public class OutsideLogic {

public static void main(String... weather) {

System.out.print(weather[0]!=null

&& weather[0].equals("sunny")
&& !false

? "Go Outside" : "Stay Inside");

1. Nothing is printed.

2. The application throws an exception at runtime.

3. Go Outside is printed.

4. Stay Inside is printed.

47. What is the value of (5 + (!2 + 8) * 3 - 3 % 2)/2 in Java?

1. 2

2. 11

3. 16

4. None of the above

48. Given the following truth table, the boolean variables w and z,

and the expression w || z, what are the missing values in the

truth table, starting with the first row?

w = true w = false

z = true true

z = false false

1. false and false

2. true and false

3. true and true

4. false and true

49. Fill in the blanks: The operators –,______________

,______________ ,______________ , and % are listed in the same

or increasing level of operator precedence.


1. +, /, *

2. --, -, *

3. ++, /, *

4. *, ++, %

50. What is the output of the following application?

public class Baby {

public static String play(int toy, int age) {

final String game;

if(toy<2)

game = age > 1 ? 1 : 10; // p1

else

game = age > 3 ? "Ball" : "Swim"; // p2

return game;

public static void main(String[] variables) {

System.out.print(play(5,2));

1. Ball

2. Swim

3. The code does not compile due to p1.

4. The code does not compile due to p2.

You might also like