Q21. which three lines are illegal? 

A. line 3 

B. line 4 

C. line 5 

D. line 6 

E. line 10 

F. line 11 

G. line 12 

H. line 13 

Answer: CDH 


Q22. Why will the code not compile? 

A. A static field cannot be private. 

B. The getLetter method has no body. 

C. There is no setLetter method. 

D. The letter field is uninitialized. 

E. It contains a method named Main instead of ma 

Answer:


Q23. Which code fragment, when inserted at line 9, enables the code to print true? 

A. String str2 = str1; 

B. String str2 = new string (str1); 

C. String str2 = sb1.toString(); 

D. String str2 = “Duke”; 

Answer:


Q24. Given: 

What is the result? 

A. The program prints nothing 

B. d 

C. A S StringIndexOutOfBoundsException is thrown at runtime. 

D. AnArrayIndexOutOfBoundsException is thrown at runtime. 

E. A NullPointerException is thrown at runtime. 

Answer:


Q25. Given the code fragment: 

String name = "Spot"; 

int age = 4; 

String str ="My dog " + name + " is " + age; 

System.out.println(str); 

And 

StringBuilder sb = new StringBuilder(); 

Using StringBuilder, which code fragment is the best potion to build and print the following 

string My dog Spot is 4 

A. sb.append("My dog " + name + " is " + age); System.out.println(sb); 

B. sb.insert("My dog ").append( name + " is " + age); System.out.println(sb); 

C. sb.insert("My dog ").insert( name ).insert(" is " ).insert(age); System.out.println(sb); 

D. sb.append("My dog ").append( name ).append(" is " ).append(age); 

System.out.println(sb); 

Answer: AD 


Q26. Given the code fragment: 

String h1 = "Bob"; 

String h2 = new String ("Bob"); 

What is the best way to test that the values of h1 and h2 are the same? 

A. if (h1 = = h2) 

B. if (h1.equals(h2)) 

C. if (h1 = = h2) 

D. if (h1.same(h2)) 

Answer:


Q27. Given the code fragment: 

nt a = 0; 

a++; 

System.out.printIn(a++); 

System.out.printIn(a); 

What is the result? 

A. 1 2 

B. 0 1 

C. 1 1 

D. 2 2 

Answer:


Q28. Given: 

A. Good Day! 

Good Luck! 

B. Good Day! 

Good Day! 

C. Good Luck! 

Good Day! 

D. Good Luck! 

Good Luck! 

E. Compilation fails 

Answer:


Q29. Given: 

What code should be inserted? 

A. Option A 

B. Option B 

C. Option C 

D. Option D 

E. Option E 

F. Option F 

Answer:


Q30. Given: 

What is the result? 

A. Null 

B. Compilation fails 

C. An exception is thrown at runtime 

D. 0 

Answer: