Skip to content

Java Object Oriented Programming Quiz 2

Java object oriented programming quiz part 2 contains 10 single choice questions. The Java OOPs questions will help you understand the OOPs concepts of the Java language. At the end of the quiz, result will be displayed along with your score and OOPs quiz answers online.

There is no time limit to complete the quiz. Click Start Quiz button to start the Java object oriented programming quiz online.

  1. 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);
    	}		
    }
    
  2. What will happen when you compile and run the following code?

    interface ITestInterface{
    	int i = 0;
    	public ITestInterface(){
    		System.out.print(" Interface Constructor ");
    	}
    }
    
    public class Test implements ITestInterface{
    	
    	public Test(){
    		System.out.print(" sub-class Constructor ");
    	}
    	
    	public static void main(String[] args){			
    		Test t = new Test();
    	}		
    }
    
  3. What will happen when you compile and run the following code?

    class One{
    	String sayHello(String name){
    		return "Hello " + name;
    	}
    	
    	char[] sayHello(String name){
    		return ("Hello " + name).toCharArray();
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){			
    		One one = new One();
    		Object object = one.sayHello("OCAJP");
    		
    		if(object instanceof String)
    			System.out.println("String");
    		else
    			System.out.println("Array");
    	}		
    }
    
  4. Java compiler always provides the default constructor.

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

    class One{
    	public static void greetings(){
    		System.out.println("Hello");
    	}
    }
    
    class Two extends One{
    	public void greetings(){
    		System.out.println("Hi");
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){		
    		One one = new Two();
    		one.greetings();
    	}		
    }
    
  6. What will happen when you compile and run the following code?

    class One{
    	public void greetings(){
    		System.out.println("Hello");
    	}
    }
    
    class Two extends One{
    	public static void greetings(){
    		System.out.println("Hi");
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){		
    		One one = new Two();
    		one.greetings();
    	}		
    }
    
  7. What will happen when you compile and run the following code?

    class One{
    	public static void greetings(){
    		System.out.println("Hello");
    	}
    }
    
    class Two extends One{
    	public static void greetings(){
    		System.out.println("Hi");
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){		
    		One one = new Two();
    		one.greetings();
    	}		
    }
    
  8. What will happen when you compile and run the following code?

    class One{
    	void One(){
    		System.out.println("Object created");
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){		
    		One one = new One();
    	}		
    }
    
  9. What will happen when you compile and run the following code?

    class One{
    	public One(int i){
    		System.out.println("int");
    	}
    	
    	public void One(short s){
    		System.out.println("short");
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){	
    		short s = 10;
    		One one = new One(s);
    	}		
    }
    
  10. What will happen when you compile and run the following code?

    class One{
    	public void className(){
    		System.out.println("Parent");
    	}
    }
    class Two extends One{
    	void className(){
    		System.out.println("Child");
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){	
    		One one = new Two();
    		one.className();
    	}		
    }