Data Types and Variables

Quiz Guidelines

 

Multiple Choice Question

QID: 
98

What happens when you try to compile and run the following class...

public class TestClass{
public static void main(String[] args) throws Exception{
int a = Integer.MIN_VALUE;
int b = -a;
System.out.println( a+ " "+b);
}
}

Select 1 option

A. It throws an OverFlowException.
B. It will print two same negative numbers.
C. It will print two different negative numbers.
D. It will print one negative and one positive number of same magnitude.
E. It will print one negative and one positive number of different magnitude.

Multiple Choice Question

QID: 
97

What happens when you try to compile and run the following program?

public class CastTest{
public static void main(String args[ ] ){
byte b = -128 ;
int i = b ;
b = (byte) i;
System.out.println(i+" "+b);
}
}

Select 1 option 

A. The compiler will refuse to compile it because i and b are of different types cannot be assigned to each other.
B. The program will compile and will print -128 and -128 when run.
C. The compiler will refuse to compile it because -128 is outside the legal range of values for a byte.
D. The program will compile and will print 128 and -128 when run.
E. The program will compile and will print 255 and -128 when run.

Multiple Choice Question

QID: 
96

What will be the result of attempting to compile and run the following code?

public class InitClass{
public static void main(String args[ ] ){
InitClass obj = new InitClass(5);
}
int m;
static int i1 = 5;
static int i2 ;
int j = 100;
int x;
public InitClass(int m){
System.out.println(i1 + " " + i2 + " " + x + " " + j + " " + m);
}
{ j = 30; i2 = 40; } // Instance Initializer
static { i1++; } // Static Initializer
}

Select 1 option

A. The code will fail to compile, since the instance initializer tries to assign a value to a static member.
B. The code will fail to compile, since the member variable x will be uninitialized when it is used.
C. The code will compile without error and will print 6, 40, 0, 30, 5 when run.
D. The code will compile without error and will print 5, 0, 0, 100, 5 when run.
E. The code will compile without error and will print 5, 40, 0, 30, 0 when run.

Multiple Choice Question

QID: 
95

Given:

public class Square {
private double side = 0; // LINE 2
public static void main(String[] args) { // LINE 4
Square sq = new Square(); // LINE 5
side = 10; // LINE 6
}
}

What can be done to make this code compile and run?

Select 1 option

A. replace // LINE 2 with:
          private int side = 0;
B. replace // LINE 2 with:
          public int side = 0;
C. replace // LINE 5 with:
         double sq = new Square();
D. replace // LINE 6 with:
          sq.side = 10;

Multiple Choice Question

QID: 
94

Given:

public class Employee{
String name;
public Employee(){
}
}

Which of the following lines creates an Employee instance?

Select 1 option

A. Employee e;
B. Employee e = new Employee();
C. Employee e = Employee.new();
D. Employee e = Employee();

Pages