Skip to content

Java Declaration Initialization and Access Control Quiz 7

Java declaration, initialization and access control quiz 7 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. Will this code compile?

    public class Test{	
    	final int i;
    }
    
  2. Will this code compile?

    public class Test{	
    	final int i;	
    	Test(){
    		i = 5;
    	}
    }
    
  3. Will this code compile?

    public class Test{	
    	final int i;
    	int j;
    	
    	Test(){
    		i = 5;
    	}
    	
    	Test(int intvalue){
    		j = intvalue;
    	}
    }
    
  4. Will this code compile?

    public class Test{	
    	static final int i;
    	
    	Test(){
    		Test.i = 5;
    	}
    }
    
  5. Will this code compile?

    public class Test{	
    	static final int i;	
    	static{
    		i = 5;
    	}
    }
    
  6. What will happen when you compile and run the following code?

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

    public class Test{
    	
    	static String name = "Test";
    
    	public Test(){
    		name = "TestObject";
    	}
    	
    	public static void main(String[] args){	
    		System.out.println("Name is " + name);
    	}	
    }
    
  8. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args){
    		
    		float f = 10.2;
    		double d = 10.2;
    		
    		if(f == d)
    			System.out.println("Same");
    		else
    			System.out.println("Not same");		
    	}	
    }
    
  9. What will happen when you compile and run the following code?

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

    import java.util.ArrayList;
    import java.util.List;
    
    public class Test{
    	
    	public static void main(String[] args){	
    		List listColors = new ArrayList<>();
    		listColors.add("Red");
    		listColors.add("Green");
    		listColors.add("Blue");
    		
    		changeMe(listColors);
    		
    		System.out.println(listColors);
    	}
    
    	private static void changeMe(final List listColors) {
    		listColors.add("Cyan");
    		listColors.remove("Blue");
    	}		
    }