OCJP Practice Papers - Operators And Decision Constructs
OCJP Practice Papers - Operators and Decision Constructs covers following topic under this section
- Using Operators and Decision Constructs
- Use Java operators; use parentheses to override operator precedence
- Test equality between Strings and other objects using == and equals()
- Create if and if/else and ternary constructs
- Use a switch statement
1.Which of the following variable types is not permitted in a switch statement?
A. String
B. double
C. int
D. char
2. What is the value of tip after executing the following code snippet?
int meal = 5;
int tip = 2;
int total = meal + (meal6 ? ++tip : tip);
A. 1
B. 2
C. 3
D. 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)));
}
}
A. true true
B. true false
C. false true
D. 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");
}
}
A. Plan A
B. Plan B
C. Plan C
D. None of the above
5. Which of the following statements about a default branch in a switch statement iscorrect?
A. All switch statements must include a default statement.
B. The default statement is required to be placed after all case statements.
C. Unlike a case statement, the default statement does not take a value.
D. 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;
A. 3
B. 4
C. 5
D. The answer cannot be determined until runtime.
7. Which statement immediately exits a switch statement, skipping all remaining case ordefault branches?
A. exit
B. break
C. goto
D. continue
8. Which statement about ternary expressions is true?
A. In some cases, both expressions to the right of the conditional operator in a ternary
expression will be evaluated at runtime.
B. Ternary expressions require parentheses for proper evaluation.
C. The ternary expressions are a convenient replacement for an if-then-else
statement.
D. 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: }
A. true
B. false
C. The code does not compile.
D. 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);
}
}
A. 2
B. 3
C. 4
D. The code does not compile.
11. Which statement about if-then statements is true?
A. An if-then statement is required to have an else statement.
B. 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.
C. An if-then statement is required to cast an object.
D. 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");
}
}
}
A. Not enough
B. Just right
C. Too many
D. None of the above
13. Which statement about case statements of a switch statement is not true?
A. A case value can be final.
B. A case statement must be terminated with a break statement.
C. A case value can be a literal expression.
D. 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
A. --
B. ++
C. ||
D. &&
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!");
A. Jump!
B. Hop!
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
16. Fill in the blanks: The _____________ operator increases the value of a variable by1 and returns the new value, while the _____________ operator decreases thevalue of a variable by 1 and returns the original value.
A. pre-increment [++v], pre-decrement [--v]
B. pre-increment [++v], post-decrement [v--]
C. post-increment [v++], pre-decrement [--v]
D. 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);
}
}
A. 11
B. 13
C. 25
D. None of the above
18. Given the following code snippet, assuming dayOfWeek is an int, what variable type ofsaturday is not permitted?
final _________ saturday = 6;
switch(dayOfWeek) {
default:
System.out.print("Another Weekday");
break;
case saturday:
System.out.print("Weekend!");
}
A. byte
B. long
C. int
D. 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";
A. Takeout
B. Salad
C. The code does not compile but would compile if parentheses were added.
D. 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");
}
}
A. Too few
B. Too many
C. The code does not compile.
D. 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);
}
}
A. 56789
B. 11789
C. 11717
D. The code does not compile.
22. Fill in the blanks: The______________ operator is used to find the differencebetween two numbers, while the______________ operator is used to find theremainder when one number is divided by another.
A. /, %
B. , %
C. %,
D. , ||
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);
}
}
A. 9
B. 11
C. 15
D. 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);
}
}
A. 1
B. 2
C. 3
D. 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 distance1000 ? "train" : 10;
}
public static void main(String[] answer) {
System.out.print(travel(500));
}
}
A. train
B. 10
C. The code does not compile.
D. 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.
A. ==, equals()
B. !=, equals()
C. equals(), ==
D. equals(), =!
27. For a given non-null String myTestVariable, what is the resulting value of executing
the statement myTestVariable.equals(null)?
A. true
B. false
C. The statement does not compile.
D. 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");
}
}
A. None
B. One
C. Two
D. The code does not compile.
29. Which statement about the logical operators & and && is true?
A. The & and && operators are interchangeable, always producing the same results at
runtime.
B. The & operator always evaluates both operands, while the && operator may only
evaluate the left operand.
C. Both expressions evaluate to true if either operand is true.
D. 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));
A. The code does not compile.
B. 10 10
C. 11 5
D. 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)));
}
}
A. true true
B. true false
C. false true
D. false false
32. What is the value of 12 + 6 * 3 % (1 + 1) in Java?
A. 0
B. 12
C. 14
D. 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
A. false and true
B. false and false
C. true and true
D. 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.length2) {
System.out.print(data[0]);
}
}
}
A. Nothing is printed.
B. sound is printed.
C. The application throws an exception at runtime.
D. logic is printed.
35. Fill in the blanks: The operators +,______________ ,______________
,______________ , and ++ are listed in the same or increasing level of operator
precedence.
A. *, --, /
B. %, -, *
C. /, *, %
D. *, -, /
36. What statement about the ^ operator is correct?
A. If one of the operands of ^ is true, then the result is always true.
B. There is a conditional form of the operator, denoted as ^^.
C. If both operands of ^ are true, the result is true.
D. 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?
A. x || z
B. y || (y && z)
C. x || y
D. 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");
}
}
}
A. long
B. double
C. int
D. 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?
A. and =
B. = and
C. and =
D. 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!");
}
}
A. Hare wins!
B. Turtle wins!
C. The code does not compile.
D. 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)+"");
}
}
A. 0
B. 1
C. 0000
D. 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));
}
}
A. up
B. middle
C. down
D. 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.
A. +, -
B. &&, !
C. |, -
D. ||, !
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 : story1 ? 2 : 1;
A. 2.0
B. 3.0
C. The code does not compile but would compile if parentheses were added.
D. None of the above
45. Fill in the blanks: A switch statement can have______________ case statementsand______________ default statements.
A. at most one, at least one
B. any number of, at most one
C. at least one, any number of
D. 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");
}
}
A. Nothing is printed.
B. The application throws an exception at runtime.
C. Go Outside is printed.
D. Stay Inside is printed.
47. What is the value of (5 + (!2 + 8) * 3 - 3 % 2)/2 in Java?
A. 2
B. 11
C. 16
D. 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
A. false and false
B. true and false
C. true and true
D. false and true
49. Fill in the blanks: The operators ,______________ ,______________
,______________ , and % are listed in the same or increasing level of operatorprecedence.
A. +, /, *
B. --, -, *
C. ++, /, *
D. *, ++, %
50. What is the output of the following application?
public class Baby {
public static String play(int toy, int age) {
final String game;
if(toy2)
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));
}
}
A. Ball
B. Swim
C. The code does not compile due to p1.
D. The code does not compile due to
OCJP Practice Test, Certification Path, Tips/Tricks
- Oracle (OCJP) Java Certification Exam
- Java Certification Tips And Tricks
- OCJP Practice Papers - Java Basics
- OCJP Practice Papers - Operators And Decision Constructs
- OCJP Practice Papers-A Advanced Java Class Design
- OCJP Practice Papers Creating And Using Arrays
- OCJP Practice Papers Using Loop Constructs
- OCJP Practice Papers - Generics And Collections
- OCJP Practice Papers - Lambda Built-In Functional Interfaces
- OCJP Practice Papers Java Class Design
- OCJP Practice Papers - Java Stream API
- OCJP Practice Papers - Exceptions And Assertions
- OCJP Practice Papers - Date/Time API
- OCJP Practice Papers - Java I/O Fundamentals
- OCJP Practice Papers - Working With Methods And Encapsulation
- OCJP Practice Papers - Working With Inheritance
- OCJP Practice Papers - Handling Exceptions
- OCJP Practice Papers - Selected Classes From Java API
- OCJP Practice Papers - Java File I/O Nio.2
- OCJP Practice Papers - Java Concurrency