public class BooleanTest {
public static void main(String[] args) {
Boolean[] bool = new Boolean[2];
bool[0] = new Boolean(Boolean.parseBoolean("true"));
bool[1] = new Boolean(null);
System.out.println(bool[0] + ", " + bool[1]);
}
}
a) true, null b) true, true c) true, false d) false, false
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) true, false Boolean.parseBoolean() returns true for "true" and false for any other value
Correct! true, false - Boolean.parseBoolean() returns true for "true" and false for any other value
2. What is the output?
public class RectAreaTest {
static double area;
int l=2, b=3;
public static void main(String[] args) {
double p, l, b;
if(area==0) {
l=3; b=4;
}
area = l * b;
System.out.println("Area = " + area);
}
}
a) Compile error b) Area = 12.0 c) Area = 6.0 d) Exception at runtime
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) Compile error local variables have to be initialized
Correct! Compile error - local variables have to be initialized
3. Which of the options causes compile error?
public class AnimalTest {
public static void main(String[] args) {
XX
System.out.println(myList);
}
}
public interface Hunter {}
public abstract class Animal { }
public class Cat extends Animal implements Hunter {}
public class Tiger extends Cat { }
A. ArrayList<Tiger> myList = new ArrayList<>();
myList.add(new Cat());
B. ArrayList<Hunter> myList = new ArrayList<>();
myList.add(new Cat());
C. ArrayList<Animal> myList = new ArrayList<>();
myList.add(new Cat());
D. ArrayList<Animal> myList = new ArrayList<>();
myList.add(new Tiger());
a) A, B
b) B
c) A
d) All of them
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) A Cat is parent of Tiger
Correct! A Cat is parent of Tiger
4. What is the output?
public class Test {
public static void main(String[] args) {
Test t =new Test();
t.printVal(23);
}
public void printVal(short val) {
System.out.println("short val = " + val);
}
public void printVal(Long val) {
System.out.println("Long val = " + val);
}
public void printVal(Object val) {
System.out.println("Object val = " + val);
}
}
a) Long val = 23 b) Object val = 23 c) Short val = 23 d) Compile error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is b) Object val = 23 : Nearest match of int is int, Integer, Number, Object in that order
Correct! Nearest match of int is int, Integer, Number, Object in that order
5. What is the output?
public class Test {
public static void main(String[] args) {
int val = 100;
Test t =new Test();
System.out.print(t.calculate(val));
System.out.println(" " + val);
}
public int calculate(int val) {
return val*2;
}
}
a) Compile error b) 200 200 c) 100 100 d) 200 100
Wrong! Try again..
Wrong Again! Try one more chance.
Wrong! The correct answer is d) 200 100 : primitive types are passed by value
Correct! 200 100 : primitive types are passed by value
6. Which of the files compile successfully?
A.java
public class A {
public void a() { }
}
B.java
public class B {
private int doIt() {
private int x=100;
return x++;
}
}
C.java
import java.io.*;
package co.abc;
class C {
public void main(String name) throws IOException {}
}
a) Only A.java b) A.java and B.java c) A.java and C.java d) All 3 of them
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) Only A.java : B has private inside method, C declares package on second line
Correct! Only A.java : B has private inside method, C declares package on second line
7. What is the output?
public class Test {
public static void main(String[] args) {
int[] arr = {3,4,5,4,5,6};
for (int i : arr) {
System.out.print(arr[i-1] + " ");
}
}
}
a) Compile error b) 3,4,5,4,5,6 c) 5 4 5 4 5 6 d) ArrayIndexOutOfBoundsException
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) 5 4 5 4 5 6 : the values at the indexes