Tuesday, 16 July 2019

Java QUIZ Part - 3

You must have gone through my tutorial on Java QUIZ part 2 here: Java QUIZ Part - 2.

In this post, I'm going to extend Java Quizzes by adding more.


Quiz 21:
    
    public class Quiz {
        public static void main(String[] args) {
            String s = "";

            StringBuffer sb1 = new StringBuffer("Hi");
            StringBuffer sb2 = new StringBuffer("Hi");
            StringBuffer sb3 = new StringBuffer(sb2);
            StringBuffer sb4 = sb3;

            if(sb1.equals(sb2)) s += "1";
            if(sb2.equals(sb3)) s += "2";
            if(sb3.equals(sb4)) s += "3";

            String s1 = "Hi";

            String s2 = "Hi";

            String s3 = s2;

            if(s1.equals(s2)) s += "4";
            if(s2.equals(s3)) s += "5";

            System.out.println(s);

        }
    }

O/P: 345

Explanation:
StringBuffer class doesn’t override equals() method.



Quiz 22:
 

    Is the following code valid override?
   
    class QuizClass{
        void add(int I, int j){

         }
    }

    class QuizSubclass extends QuizClass{
        public void add(int I, int j){

        }
    }




O/P : true

Explanation:
In subclass, we can assign higher access privilege.


Quiz 23:

    
    Is the following code valid override?

    class QuizClass{
        private void add(int i, int j){

        }
    }

    class QuizSubclass extends QuizClass{
        public void add(int i, int j){

        }
    }

    O/P : true

    Note: But, you cannot call add() method on QuizClass as it is private.
    e.g.
    QuizClass qc = new QuizSubclass();
    qc.add(1,2);// Cannot call, as add() is private in QuizClass.



Quiz 24:

    
    Is the following code valid override?

    class QuizClass{
        static void add(int i, int j){

        }
    }

    class QuizSubclass extends QuizClass{
        static void add(int i, int j){

        }
    }



    O/P : It is valid.
    
    Note: 
    But static methods cannot be overridden. It is called method hiding.
    So, when we use below code:
    QuizClass qc = new QuizSubclass();
    qc.add(1,2);

    It calls add() method of QuizClass.



Quiz 25:
    
    Is the following code valid override?

    class QuizClass{
        static void add(int i, int j) throws Exception{

        }

    }

    class QuizSubclass extends QuizClass{
        static void add(int i, int j) throws IOException{

        }
    }

    O/P : It is valid.
    
    Explanation:
    
    Overridden method can throw the subclass of Exception[or same exception] thrown by 
    method in parent class.



Quiz 26:

    
    Is the following code valid override?

    class QuizClass{
        static void add(int i, int j){


        }
    }

    class QuizSubclass extends QuizClass{
        static void add(int i, int j){

        }
    }

    O/P : It is valid.



Quiz 27:

    
    Is the following code valid override?
    
    class QuizClass{
        protected void add(int i, int j) throws Exception{

        }
    }

    class QuizSubclass extends QuizClass{
        public void add(int i, int j) throws IOException{

        }
    }

    O/P :  Not valid. Compiler error. Attempting to assign weaker access privilege.



Quiz 28:

    class QuizA{

    }

    
    class QuizB extends QuizA{

    }

    
    class QuizC extends QuizB{

    }

    public class MainClass{
        static void overloadedMethod(QuizA a){
            System.out.println("ONE");
        }

        
        static void overloadedMethod(QuizB b){
            System.out.println("TWO");
        }

        
        static void overloadedMethod(Object obj){
            System.out.println("THREE");
        }

        public static void main(String[] args){
            QuizC c = new QuizC();
            overloadedMethod(c);
        }

    }

    O/P: TWO

    Explanation: In method overloading, more specific method is chosen over generic.



Quiz 29:
    
    public class MainClass{
        static void overloadedMethod(Integer I, int i){
            System.out.println("ONE");
        }

        
        static void overloadedMethod(int i, Integer I){
            System.out.println("TWO");
        }

        
        static void overloadedMethod(Object obj){
            System.out.println("THREE");
        }

        public static void main(String[] args){
            overloadedMethod(1,2);
        }

    }

    O/P: Compile-time error


Quiz 30:

    Question: In a class, one method has two overloaded forms. One form is defined as static and   
    another form is defined as non-static. Is that method properly overloaded?

    Answer: Yes. Compiler checks only method signature to verify whether a particular method is 
    properly overloaded or not. It doesn't check static or non-static feature of method.


    If you really like this post, press Hit on 'Like'.



No comments:

Post a Comment