Q1. Given:
What is the result?
A. hEllOjAvA!
B. Hello java!
C. Out of limits hEllOjAvA!
D. Out of limits
Answer: C
Q2. Given:
public class Test {
public static void main(String[] args) {
int day = 1;
switch (day) {
case "7": System.out.print("Uranus");
case "6": System.out.print("Saturn");
case "1": System.out.print("Mercury");
case "2": System.out.print("Venus");
case "3": System.out.print("Earth");
case "4": System.out.print("Mars");
case "5": System.out.print("Jupiter");
}
}
}
Which two modifications, made independently, enable the code to compile and run?
A. Adding a break statement after each print statement
B. Adding a default section within the switch code-block
C. Changing the string literals in each case label to integer
D. Changing the type of the variable day to String
E. Arranging the case labels in ascending order
Answer: A,C
Explanation: The following will work fine:
public class Test {
public static void main(String[] args) {
int day = 1;
switch (day) {
case 7: System.out.print("Uranus"); break;
case 6: System.out.print("Saturn"); break;
case 1: System.out.print("Mercury"); break;
case 2: System.out.print("Venus"); break;
case 3: System.out.print("Earth"); break;
case 4: System.out.print("Mars"); break;
case 5: System.out.print("Jupiter"); break;
}
}
}
Q3. Given the code fragment:
Which two modifications, made independently, enable the code to compile?
A. Make the method at line n1 public.
B. Make the method at line n2 public.
C. Make the method at line n3 public.
D. Make the method at line n3 protected.
E. Make the method at line n4 public.
Answer: C,D
Q4. Given:
What is the result?
A. 2 4 6 8 10 12
B. 2 4 6 8 10 12 14
C. Compilation fails
D. The program prints multiple of 2 infinite times
E. The program prints nothing
Answer: B
Q5. Given:
public class MyClass {
public static void main(String[] args) {
String s = " Java Duke ";
int len = s.trim().length();
System.out.print(len);
}
}
What is the result?
A. 8
B. 9
C. 11
D. 10
E. Compilation fails
Answer: B
Explanation: Java - String trim() Method
This method returns a copy of the string, with leading and trailing whitespace omitted.
Q6. Given the code fragment:
StringBuilder sb = new StringBuilder ( ) ;
Sb.append (“world”);
Which code fragment prints Hello World?
A. sb.insert(0,"Hello ");
System.out.println(sb);
B. sb.append(0,"Hello ");
System.out.println(sb);
C. sb.add(0,"Hello ");
System.out.println(sb);
D. sb.set(0,"Hello ");
System.out.println(sb);D
Answer: A
Explanation: The java.lang.StringBuilder.insert(int offset, char c) method inserts the string representation of the char argument into this sequence. The second argument is inserted into the contents of this sequence at the position indicated by offset. The length of this sequence increases by one.The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.
Reference: Java.lang.StringBuilder.insert() Method
Q7. Which of the following can fill in the blank in this code to make it compile? (Select 2 options.)
A. On line 1, fill in throws
B. On line 1, fill in throws new
C. On line 2, fill in throw new
D. On line 2, fill in throws
E. On line 2, fill in throws new
Answer: A,C
Explanation:
Option A and C are the correct answer.
In a method declaration, the keyword throws is used. So here at line 1 we have to use
option A.
To actually throw an exception, the keyword throw is used and a new exception is created,
so at line 2 we have to use throw and new keywords, which is option C. Finally it will look
like;
public void method() throws Exception {
throw new Exception0;
}
REFERENCE : httpsy/docs.oracle.com/javase/tutorial/essential/io/fileOps.html#exception
The correct answer is: On line 1, fill in throws. On line 2, fill in throw new
Q8. Which two are benefits of polymorphism?
A. Faster code at runtime
B. More efficient code at runtime
C. More dynamic code at runtime
D. More flexible and reusable code
E. Code that is protected from extension by other classes
Answer: C,D
Q9. Given the code fragment:
What is the result?
A. Found Red
Found Default
B. Found Teal
C. Found Red
Found Blue
Found Teal
D. Found Red
Found Blue
Found Teal
Found Default
E. Found Default
Answer: B
Q10. Given the code fragment:
Which two modifications should you make so that the code compiles successfully?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Answer: A,C
Explanation:
Add throws clause in both printFileContent and main.
Q11. Given:
public class Marklist {
int num;
public static void graceMarks(Marklist obj4) {
obj4.num += 10;
}
public static void main(String[] args) {
MarkList obj1 = new MarkList();
MarkList obj2 = obj1;
MarkList obj1 = null;
obj2.num = 60;
graceMarks(obj2);
}
}
How many objects are created in the memory runtime?
A. 1
B. 2
C. 3
D. 4
Answer: B
Explanation: obj1 and obj3.
when you do e2 = e1 you're copying object references - you're not making a copy of the object - and so the variables e1 and e2 will both point to the same object.
Q12. Given the code format:
Which code fragment must be inserted at line 6 to enable the code to compile?
A. DBConfiguration f; return f;
B. Return DBConfiguration;
C. Return new DBConfiguration;
D. Retutn 0;
Answer: B
Q13. Which code fragment cause a compilation error?
A. flat flt = 100F;
B. float flt = (float) 1_11.00;
C. float flt = 100;
D. double y1 = 203.22; floatflt = y1
E. int y2 = 100;
floatflt = (float) y2;
Answer: B
Q14. Which of the following will print current time?
A. System.out.print(new LocalTime()-now0);
B. System.out.print(new LocalTime());
C. System.ouLprint(LocalTime.now());
D. System.ouLprint(LocalTime.today());
E. None of the above.
Answer: C
Explanation:
The LocalTime is an interface, so we can't use new keyword with them. So options A and C are incorrect. To get current time we can call now method on LocalTime interface. So option C is correct. Option D is incorrect as there is no method called today as in LocalTime interface
https://docs.oracle.com/javase/tutorial/datetime/iso/datetime.html
Q15. Given the code fragment:
What is the result?
A. true true
B. true false
C. false false
D. false true
Answer: C