Question1: What is the result of below program?
public class TestVariables { int x,y; public TestVariables(int x,int y) { intialize(x,y); } public void intialize(int x,int y) { this.x = x*x; this.y=y*y; } public static void main(String[] args) { int x=3,y=5; TestVariables test = new TestVariables(x,y); System.out.println(x+ " "+y); } }
A. Compilation fails.
B. 3 5
C. 0 0
D. 9 25
Answer: B
Question2: What is the result of the below program?
public class SwitchTest { public static void main(String[] args) { int wd=0; String days[] = {"sun","mon","wed","sat"}; for(String s:days) { switch(s) { case "sat": case "sun": wd-=1; break; case "mon": wd++; case "wed": wd+=2; } } System.out.println(wd); } }
A. 3
B. 4
C. -1
D. Compilation fails.
Answer: A
Question3: Given below program:
public class MyFile { public static void main(String[] args) { String arg1 = args[1]; String arg2 = args[2]; String arg3 = args[3]; System.out.println("Arg is"+arg3); } }
Which command-line arguments should you pass to the program to obtain the following output? Arg is 2
A. java MyFile 1 3 2 2
B. java MyFile 2 2 2
C. java MyFile 1 2 2 3 4
D. java MyFile 0 1 2 3
Answer: A
Question4: Given below code :
StringBuilder sb1 = new StringBuilder();
String str1 = sb1.toString();
// Insert Code Here
System.out.println(str1 == str2);
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: A
Question5: Given the below program:
public class TestCal { public void updatePrice(Product product,double price) { price = price *2; product.price = product.price + price; } public static void main(String[] args) { Product product = new Product(); product.price = 200; double price = 100; TestCal test = new TestCal(); test.updatePrice(product, price); System.out.println(product.price+":"+price); } }
What is the result?
A. 200.0 : 100.0
B. 400.0 : 200.0
C. 400.0 : 100.0
D. Compilation fails.
Answer: C
Question6: Given the below
public class Test { public static void main(String[] args) { boolean a = new Boolean(Boolean.valueOf(args[0])); boolean b = new Boolean(args[1]); System.out.println(a + "" + b); } } javac Test.java java Test 1 null
What is the result?
A. 1 null
B. true false
C. false false
D. true true
E. A ClassCastException is thrown at runtime.