Monday, 15 July 2019

Java QUIZ Part - 2

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

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

Quiz 11:

    public class Quiz{
        static String str;
     
        public static Boolean method1(){
            return new Boolean("1");
        }
     
        public static Boolean method2(){
            return new Boolean(str);
        }

        public static void main(String[] args){
            if(method1())
                System.out.print("1");
            if(!method2())
                System.out.print("2");
            if(method1() != method2())
                System.out.print("3");
       }

    }

O/P: 2

Explanation:
Boolean returns false for strings which consists of null and text that is not true.
So, in method1() , "1" is not true and in method2(), str is null , so it returns false in both cases.


Quiz 12:

    public class Quiz{
        public static void main(String[] args){
            int k = 0;
            int[] nums = {1,2,3,5};
         
            for(int i : nums){
                switch(i){
                    case 1:
                        k += i;
                    case 5:
                        k += i;
                    default:
                        k += i;
                    case 2:
                        k += i;
                }
            }
            System.out.println(k);
        }
    }

O/P:  27

Explanation:

For case i = 1: All cases will run
For case i = 2: Only case 2 will run
For case i = 3: Default case and case 2 will run
For case i = 5: Case 5, default case and case 2 will run

Note: When a case is matched in switch, its code and all subsequent case code will run unless break is encountered.





Quiz 13:

    public static void main(String[] args){
        Set<Short> set = new HashSet<>();
     
        for(short i = 0; i< 10; i++){
            set.add(i);
            set.remove(i-1);
        }

        System.out.println(set.size());

    }

O/P: 10

Explanation:
We are adding short type in Set. But are removing integers. (i-1) is an integer.

 

Quiz 14:

    public class Names{
        private final String strName;
     
        public Name(String name){
            this.strName = name;
        }

        public boolean equals(Object obj){
            if(!(obj instanceof Names))
                return false;
            Names n = (Names)obj;
            return n.strName.equals(strName);

        }

        public static void main(String[] args){
            Set<Name> set = new HashSet<Name>;
            set.add(new Names("Javafreeway"));
            System.out.println(set.contains(new Names("Javafreeway")));

        }
    }

O/P: false;

Explanation:
This is because hashcode() method is not overridden. And default hashcode() implementation in Object class compares memory addresses.


Quiz 15:

    private static String code(String strCode){
        try{
            return "hello";
        }
        finally{
            return strCode;
        }
    }

    public static void main(String[] args){
        System.out.println(code("javafreeway"));
    }

O/P: javafreeway

Explanation:

finally block overrides the value returned by try and catch blocks.



Quiz 16:

    public class Quiz{
        private static int increment(int i){
            int num = (++i) + (i++);
            return num;
        }

        public static void main(String[] args){
            for(int i=0; i<5; increment(i)){
                System.out.println(i);
            }

        }
    }

O/P: Infinite loop

Explanation:
3rd parameter of for loop will not get increased. So, value of i will remain 0.



Quiz 17:
 
    public class Quiz{
        private static int increment(int i){
            int num = (++i) + (i++);
            return num;
        }

        public static void main(String[] args){
            for(int i=0; i<5; i = increment(i)){
                System.out.println(i);   // Line 1
            }

        }
    }

O/P: 02

Explanation:
First of all, Line 1 gets execute and prints 0.
Then increment() gets called and returns 2.
Then 2 gets printed in Line 1.
Then increment() gets called and returns 6 which is > 5 and exits.



Quiz 18:

    public class SuperClazz{
        protected SuperClazz(){
            System.out.print("super");
        }
    }

    public class SubClazz extends SuperClazz{
        private SubClazz(){
            System.out.print("sub");
        }

        public static void main(String[] args){
            new SuperClazz();
            class InnerClazz{
                private InnerClazz(){
                    System.out.print("inner");    
                }
                {
                     new SubClazz();
                }

                {
                     new SuperClazz();
                }
            }
            new InnerClazz();
        }
    }
 
O/P: super super sub super inner


Quiz 19:

    public class QuizA{
        String hello(){
            return "A";
        }
   }

    public class QuizB extends QuizA{
     
        String hello(){
            return "B";
        }
    }

  
    public class QuizC extends QuizB{
        
        String hello(){
            return "C";
        }

        public static void main(String[] args){
            QuizA[] quizzes = new QuizA[3];
            
             quizzes[0] = new QuizB();
             quizzes[1] = (QuizA) new QuizC();
             quizzes[2] = (QuizA) new QuizB();

             for(QuizA a : quizzes)
                 System.out.print(a.hello());

        } 
    }

O/P: BCB



Quiz 20:

    public class SuperQuiz{
        static String str = "";
        static{
            str += "1";
        }
        public SuperQuiz(){
            str += "2";
        }
    }

    public class SubQuiz extends SuperQuiz{
        SubQuiz(){
            str +=  "3";
        }

        static{
            new SubQuiz();
            System.out.println(str);
        }

        {
             str += "4";
        }

        public static void main(String[] args){

        }

    }

O/P: 1243


   



No comments:

Post a Comment