Skip to content

Java Declaration Initialization and Access Control Quiz 9

Java declaration, initialization and access control quiz 9 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{
    	
    	public static void main(String[] args) {
    		int[] intArray;
    		System.out.println(intArray[0]);
    	}
    }
    
  2. What will happen when you compile and run the following code?

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

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

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

    public class Test{	
    	
    	static int[] intarray = new int[5];
    	
    	public static void main(String[] args) {
    		for(int i = 0; i < intarray.size(); i++)
    			System.out.print(intarray[i]);
    	}
    }
    
  6. Will this code compile?

    abstract class Test{	
    	public Test(){
    		System.out.println("Constructor");
    	}
    }
    
  7. What will happen when you compile and run the following code?

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

    import java.util.ArrayList;
    
    public class Test {
    	
    	public static void main(String[] args){		
    		
    		ArrayList aListColors = new ArrayList<>();
    		aListColors.add("Red");
    		aListColors.add("Blue");
    		aListColors.add(5*5);
    		
    		System.out.println(aListColors);
    	}
    }
    
  9. What will happen when you compile and run the following code?

    import java.util.ArrayList;
    
    public class Test {
    	
    	public static void main(String[] args){		
    		
    		ArrayList aListNumbers = new ArrayList<>();
    		aListNumbers.add(1);
    		aListNumbers.add(2);
    		System.out.println( aListNumbers.get(0)+aListNumbers.get(1) );
    		
    	}
    }
    
  10. What will happen when you compile and run the following code?

    public class Test{	
    	
    	static boolean b;
    	
    	public static void main(String[] args) {
    		if(b = false)
    			System.out.println(false);
    		else
    			System.out.println(true);
    	}
    }