Java Declaration Initialization and Access Control Quiz 2

Java declaration, initialization and access control quiz 2 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 quiz online.

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

    import java.io.FileNotFoundException;
    
    interface TestInterface{
    	void sayHello();
    }
    
    public class Test implements TestInterface{
    	public void sayHello() throws FileNotFoundException{	
    		System.out.println("File Not Found");
    	}
    	
    	public static void main(String[] args) throws FileNotFoundException {
    		TestInterface t = new Test();
    		t.sayHello();
    	}
    }
    
  2. What will happen when you compile and run the following code with below given command?

    java Test 1 2

    interface ITestInterface{
    	int status = 0;
    }
    
    public class Test implements ITestInterface{
    	
    	public static void main(String[] args){
    		
    		if(args[0] == "1")
    			status = 0;
    		else if(args[0] == "2")
    			status = 1;
    		
    		System.out.print(status);
    	}		
    }
    
  3. Will this code compile without any errors?

    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    interface TestInterface{
    	void sayHello() throws IOException;
    }
    
    public class Test implements TestInterface{
    	public void sayHello() throws FileNotFoundException{	
    	}
    	
    	public static void main(String[] args) throws FileNotFoundException {
    		Test t = new Test();
    		t.sayHello();
    	}
    }
    
  4. What will happen when you compile and run the following code?

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

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

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

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

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

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

    public class Test{
    	
    	public static void main(String[] args){	
    		
    		int i = 10;
    		
    		if( i < 10){
    			String str = "Less";
    		}else{
    			String str = "Not Less";
    		}
    		
    		System.out.println(str);
    
    	}
    }
    

Online Shopping