Skip to content

Java Declaration Initialization and Access Control Quiz 5

Java declaration, initialization and access control quiz 5 contains 10 single 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?

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

    public class Test{
    	
    	public static void main(String[] args) {
    		
    		boolean b1 = true, b2 = False;
    		if(b1 & b2)
    			System.out.println("1");
    		else
    			System.out.println("2");
    	}
    }
    
  3. A subclass must implement all the abstract methods declared by its parent abstract class.

  4. Will this code compile successfully?

    import java.util.ArrayList;
    
    public class Test{
    	
    	public static void main(String[] args) {
    		ArrayList aList = new ArrayList();
    		aList.add(1);
    	}
    }
    
  5. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args) {
    		boolean hot, cold=false;
    		System.out.println(hot && cold);
    	}
    }
    
  6. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args) {
    		Integer i1 = new Integer(2), i2 = new Integer(2);
    		if(i1 == i2)
    			System.out.print("true");
    		else
    			System.out.print("false");	
    		
    	}
    }
    
  7. What will happen when you compile and run the following code?

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

    public class Test{	
    	final int i;
    	public static void main(String[] args){		
    		System.out.println(i);
    	}		
    }
    
  9. A variable declared by an interface can be changed by the class implementing it.

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

    public class Test{
    	
    	static String str = "false";
    	
    	public static void main(String[] args) {
    		if(str == false)
    			System.out.println("Nothing");
    	}
    }