Java Mock Test 1

Java Mock Test 1

Java Mock Test 1, 10 QUESTIONS

published on May 18, 2009238 responses 10 2.6★ / 5

What will happen if you try to compile and run the following code?
------
public class MyClass {
public int x = 10;
public static int y = 10;
public static void main(String []args){
System.out.println(x==y);
}
}

true
false
compilation error
runtime error

What will happen if you try to compile and run the following code?
------
public class MyClass {

public static void main(String []args){
byte a = 10;
byte b = 12;
byte c = a + b;
System.out.println(c);
}
}

22
Compilation Error
Runtime Error

What will happen if you try to compile and run the following code?
------
public class MyClass {

public static void main(String []args){
byte a = 10;
int b = 12;
b+=a;
System.out.println(b);
}
}

Compilation Error
Runtime Error
10
12
22

What will happen if you try to compile and run the following code?
------
public class MyClass {

public static void main(String []args){
boolean a = true;
boolean b = false;
if(b = a){
System.out.println(a+":"+b);
}
}
}

true:false
true:true
false:true
false:false
Compilation Error
Runtime Error

What will happen if you try to compile and run the following code?
------
public class MyClass {

public static void main(String []args){
boolean a = "true".toBoolean();
boolean b = false;
boolean c = a || b;

System.out.println(c);
}
}

true
false
Compilation Error
Runtime Error

Which of the following statements are correct about "myFunction"?
------
public static void myFunction(String []args){
boolean []a = new boolean [0];
boolean []b = new Boolean [0];
boolean []c = new int [0];
int []d = new boolean[0];
}

Select the two correct answers
The code will compile and execute successfully.
You cannot create a 0 length array.
You cannot assign an array of Boolean objects to an array of boolean primitives.
You cannot assign an array of integers to an array of boolean primitives.
The code will compile successfully but will generate an exception at execution.

What will be displayed when the following code is compiled and executed?
------
int i , j = 12;
System.out.println("i="+i);
System.out.println("j="+j);

i=12
j=12
1=0
j=12
i=null
j=12
Compilation Error
Execution Error

What will be displayed after the following code is compiled and executed?
------
String colour = "WHIte";

switch (colour) {
case "white":
System.out.println("white");
case "WHITE":
System.out.println("WHITE");
case "WhitE":
System.out.println("WhitE");
default:
System.out.println("default");
}

white
WHITE
WhitE
default
Compilation Error
Runtime Error

What will be displayed after the following code is compiled and executed?
------
long x = 3;
switch (x) {
case 3:
System.out.println(3);
case 2:
System.out.println(2);
case 1:
System.out.println(1);
break;
default:
System.out.println("default");
}

3
2
1
default
Compilation Error
Runtime Error

What will happen if you try to compile and run the following code?
------
int x = 3;
switch (x) {
default:
System.out.print("default");
case 3:
System.out.print(3);
case 2:
System.out.print(2);
case 1:
System.out.print(1);
}