Java Declaration Initialization and Access Control Quiz 3

Java declaration, initialization and access control quiz 3 contains 10 single choice questions. This 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 quiz online.

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

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

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

    public class Test{
    	
    	public static void main(String[] args){	
    		Integer i;
    		System.out.println(i);
    	}
    }
    
  4. Which of the below given variable names are valid in Java?

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

    class One{
    	public One(){
    		System.out.print("One");
    	}
    }
    class Two extends One{
    	public Two(){
    		System.out.print("Two");
    	}
    }
    class Three{
    	Two two = new Two();
    	public Three(){
    		System.out.print("Three");
    	}
    }
    public class Test{	
    	
    	public static void main(String[] args) {
    		Three three = new Three();
    	}
    }
    
  6. Can you override public constructor declared in a base class to private in a child class?

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

    public class Test{
    	
    	static int i = 0;
    	
    	static{
    		i += 1;
    	}
    	
    	public static void main(String[] args){	
    		System.out.println(i);
    	}
    	
    	static{
    		i += 1;
    	}
    }
    
  8. All the methods declared in an interface are implicitly public.

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

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

    public class Test{
    	
    	static int i = 0;
    	
    	{
    		i += 1;
    	}
    	
    	public static void main(String[] args){	
    		System.out.print(i);
    		Test t = new Test();
    		System.out.print(i);
    	}
    	
    	static{
    		i += 1;
    	}
    }