Java Object Oriented Programming Quiz 1

Java object oriented programming quiz part 1 contains 10 single and multiple choice questions. Java object oriented programming questions are designed in such a way that it will help you understand 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?

    class One{
    	int i = 1;
    	public int getInt(){
    		return i;
    	}
    }
    
    class Two extends One{
    	int i = 2;
    	public int getInt(){
    		return i;
    	}	
    }
    public class Test{
    	public static void main(String[] args) {
    		One one = new One();
    		Two two = (Two)one;
    		System.out.println( two.getInt() );
    	}
    }
    
  2. What will happen when you compile and run the following code?

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

    class One{	
    	public static void print(int i){
    		System.out.println("Parent");
    	}
    }
    
    class Two extends One{
    	public static void print(byte b){
    		System.out.println("Child");
    	}
    }
    
    public class Test{
    	
    	public static void main(String args[]){
    		One one = new Two();
    		one.print(10);
    	}	
    }
    
  4. Will this code compile successfully?

    class One{
    	public void process(){
    		System.out.println("Parent");
    	}
    }
    
    public abstract class Test extends One{
    	public abstract void process();
    }
    
  5. What option if written at the line 12 will compile and run without any errors?

    interface Inter{}
    class One{}
    class Two extends One{}
    class Three extends One{}
    class Four extends Two implements Inter{} 
    class Five extends Four{}
    class Six extends Three{}
    
    public class Test{
    	
    	public static void main(String[] args) {		
    		//your code here
    	}
    }
    
  6. What will happen when you compile and run the following code?

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

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

    class One{
    	
    	public One(int x){
    		System.out.print("int constructor");
    	}
    
    	public One(long l){
    		System.out.print("long constructor");
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){
    		long l = 20l;
    		One one = new One(l);		
    	}		
    }
    
  9. What will happen when you compile and run the following code?

    class One{
    	
    	public One(){}
    	
    	public One(long l){
    		System.out.print("long constructor");
    	}
    }
    
    class Two extends One{
    	public One(int i){
    		System.out.print("int constructor");
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){
    		long l = 20l;
    		One one = new Two(l);		
    	}		
    }
    
  10. What will happen when you compile and run the following code?

    class One{
    	
    	public One(int x){
    		System.out.print("int constructor");
    	}
    
    	public One(char c){
    		System.out.print("char constructor");
    	}
    	
    	void One(String str){
    		System.out.print("String constructor");
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){
    		String ch = "c";
    		One one = new One(ch);		
    	}		
    }