Q1. Given the code fragment:
What is the result?
A. Match 1
B. Match 2
C. No Match
D. A NullPointerException is thrown at runtime.
Answer: B
Explanation:
it will compare the string contents of the StringBuilder with string object.
Q2. Given:
What is the result?
A. Red 0
Orange 0
Green 3
B. Red 0
Orange 0
Green 6
C. Red 0
Orange 1
D. Green 4
E. Compilation fails
Answer: E
Q3. Given the code fragment:
Which option can replace xxx to enable the code to print 135?
A. int e = 0; e < = 4; e++
B. int e = 0; e < 5; e + = 2
C. int e = 1; e < = 5; e + = 1
D. int e = 1; e < 5; e+ =2
Answer: B
Q4. Given:
What is the output?
A. 2015-03-27
B. 2015-04-27
C. 2015-02-27 D. Compilation fails due to error at line 6.
E. Compilation fails due to error at line 8.
Answer: A
Explanation:
To create we have used following method with LocalDate class;
public static LocalDate of(intyear, int month, int dayOfMonth)
Here we need to remember that month is not zero based so if you pass 1 for month, then
month will be January.
Then we have used period object of 1 day and add to date object which makes current date
to next day, so final output is 2015-03-27. Hence option A is correct.
https://docs.oracle.com/javase/tutorial/datetime/iso/datetime.html
Q5. Given the definitions of the MyString class and the Test class:
What is the result?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: C
Q6. What is the name of the Java concept that uses access modifiers to protect variables and hide them within a class?
A. Encapsulation
B. Inheritance
C. Abstraction
D. Instantiation
E. Polymorphism
Answer: A
Q7. What is the proper way to defined a method that take two int values and returns their sum as an int value?
A. int sum(int first, int second) { first + second; }
B. int sum(int first, second) { return first + second; }
C. sum(int first, int second) { return first + second; }
D. int sum(int first, int second) { return first + second; }
E. void sum (int first, int second) { return first + second; }
Answer: D
Q8. Given the code fragment:
What is the result?
A. [Robb, Rick, Bran]
B. [Robb, Rick]
C. [Robb, Bran, Rick, Bran]
D. An exception is thrown at runtime.
Answer: A
Q9. Given the code fragments:
Which code fragment, when inserted at line ni, enables the code to print Hank?
A. checkAge (iList, ( ) -> p. get Age ( ) > 40);
B. checkAge(iList, Person p -> p.getAge( ) > 40);
C. checkAge (iList, p -> p.getAge ( ) > 40);
D. checkAge(iList, (Person p) -> { p.getAge() > 40; });
Answer: C
Q10. Given:
What is the result?
A. A B C D
B. A C D
C. A B C
D. A B D
E. A B D C
Answer: C
Q11. Given:
A. a, e
i, o
B. a, e
o, o
C. e, e
I, o
D. e, e
o, o
Answer: B
Q12. Given:
And given the code fragment:
What is the result?
A. 4W 100 Auto 4W 150 Manual
B. Null 0 Auto 4W 150 Manual
C. Compilation fails only at line n1
D. Compilation fails only at line n2
E. Compilation fails at both line n1 and line n2
Answer: E
Explanation:
On line n1 implicit call to parameterized constructor is missing and n2 this() must be the first line.
Q13. Given the code fragment:
// insert code here
arr[0] = new int[3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1] = new int[4];
arr[1][0] = 10;
arr[1][1] = 20;
arr[1][2] = 30;
arr[1][3] = 40;
Which two statements, when inserted independently at line // insert code here, enable the code to compile?
A. int [] [] arr = null;
B. int [] [] arr = new int [2];
C. int [] [] arr = new int [2] [ ];
D. int [] [] arr = new int [] [4];
E. int [] [] arr = new int [2] [0];
F. int [] [] arr = new int [0] [4];
Answer: C,E
Q14. Given the code fragment:
Which three code fragments can be independently inserted at line nl to enable the code to print one?
A. Byte x = 1;
B. short x = 1;
C. String x = "1";
D. Long x = 1;
E. Double x = 1;
F. Integer x = new Integer ("1");
Answer: A,B,F
Q15. Given:
class Sports {
int num_players;
String name, ground_condition;
Sports(int np, String sname, String sground){
num_players = np;
name = sname;
ground_condition = sground;
}
}
class Cricket extends Sports {
int num_umpires;
int num_substitutes;
Which code fragment can be inserted at line //insert code here to enable the code to compile?
A. Cricket() {
super(11, "Cricket", "Condidtion OK");
num_umpires =3;
num_substitutes=2;
}
B. Cricket() {
super.ground_condition = "Condition OK";
super.name="Cricket";
super.num_players = 11;
num_umpires =3;
num_substitutes=2;
}
C. Cricket() {
this(3,2);
super(11, "Cricket", "Condidtion OK");
}
Cricket(int nu, ns) {
this.num_umpires =nu;
this.num_substitutes=ns;
}
D. Cricket() {
this.num_umpires =3;
this.num_substitutes=2;
super(11, "Cricket", "Condidtion OK");
}
Answer: A
Explanation:
Incorrect:
not C, not D: call to super must be the first statement in constructor.