Skip to content

Java Declaration Initialization and Access Control Quiz 1

Java declaration, initialization and access control quiz 1 contains 10 single and multiple choice questions. Declaration, initialization and access control quiz questions are designed in such a way that it will help you understand how to declare and initialize variables in Java as well as how to properly define access control for class, member variables and methods. At the end of the quiz, result will be displayed along with your score and quiz answers.

There is no time limit to complete the quiz. Click Start Quiz button to start the Java Declaration, Initialization and Access Control quiz online.

  1. What will happen when you compile and run the following code?

    public class Test{	
    	static int i;	
    	public static void main(String[] args) {
    		Test t = null;
    		System.out.println(t.i);
    	}
    }
    
  2. What will happen when you compile and run the following code?

    class One{
    	public One(){
    		System.out.print("One");
    	}
    }
    
    class Two{
    	public Two(){
    		System.out.print("Two");
    	}
    }
    
    class Three{
    	public Three(){
    		System.out.print("Three");
    	}
    }
    public class Test{
    	
    	static Three three = new Three();
    	Two two = new Two();
    	static One one = new One();
    	
    	public static void main(String[] args) {
    		System.out.print("Hello");
    		Test t = new Test();
    	}
    }
    
  3. What will happen when you compile and run the following code?

    class One{
    	public One(){
    		System.out.println("One");
    	}
    }
    
    public class Test{
    	
    	One one = new One();
    	
    	public static void main(String[] args) {
    		System.out.println("Hello");
    	}
    }
    
  4. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    
    		int i = 97;
    		
    		switch(i){
    			case 95 :
    				System.out.print("95");
    			case 96 :
    				System.out.print("96");
    			case 'a' :
    				System.out.print("a");
    			case 'b' :
    				System.out.print("b");
    			case 'c' :
    				System.out.print("c");
    		}
    	}	
    }
    
  5. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int a = 0;
    		while(a + 2 < 10){
    			a += 2;
    			int b = 10;
    			while(b - 2 > 0){
    				b -= 2;
    			}				
    		}
    		System.out.println(a + " " + b);
    	}
    	
    }
    
  6. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    
    		for(int i = 0 ; i < 10; i++){			
    		}		
    		System.out.println(i);
    	
    	}
    	
    }
    
  7. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args){
    		
    		double d1 = Double.NaN;		
    		double d2 = d1;
    		
    		if(d1 == d2)
    			System.out.println("Equal");
    		else
    			System.out.println("Not Equal");
    	}
    	
    }
    
  8. What will happen when you compile and run the following code?

    interface TestInterface{
    	void sayHello();
    }
    
    public class Test implements TestInterface{
    	void sayHello(){
    		System.out.println("Hello");
    	}
    	
    	public static void main(String[] args) {
    		Test t = new Test();
    		t.sayHello();
    	}
    }
    
  9. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args){			
    		int i = 5, j = null;
    		System.out.println( i % j );		
    	}	
    }
    
  10. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int i = 10;
    		int j = 25;
    		System.out.println(i + ' ' + j);
    	}
    }