Skip to content

Java Declaration Initialization and Access Control Quiz 10

Java declaration, initialization and access control quiz 10 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. A top level class can be private, protected or public.

  2. A private member variable or method is only accessible in the same class as well as in the sub-classes.

  3. A member variable or method declared without any access modifier is only accessible in the same class and all its sub-classes.

  4. A member variable or method declared with the public access modifier is only accessible in all the classes in the same package.

  5. A member variable or method declared with protected access modifier is only accessible in all the sub-classes in the same package.

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

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

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

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

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

    public class Test {
    	
    	public static void main(String[] args){		
    
    	Long l1 = new Long(7), l2 = new Long(7);
    	if(l1 == l2)
    		System.out.print("true ");
    	else
    		System.out.print("false ");
    	
    	if(l1.equals(l2))
    		System.out.print("true");
    	else
    		System.out.print("false");
    	
    	}
    }