Java (Basic) Certification MCQs | HackerRank

  1. What is the output of following program?
    public class BitPuzzel {
        public static void main(String[] args) {
            int mask = 0x000F;
            int value = 0x2222;
            System.out.println(value & mask);
        }
    }
Answer: 2
  1. What is the output of following program?
    public class A {
        int add(int i, int j) {
            return i + j;
        }
    }
    public class B extends A {
        public static void main(String[] args) {
            short s = 9;
            System.out.println(add(s, 6));
        }
    }
Answer: Compilation Fails due to error at line 9, non-static method referenced from a static context
  1. Which is the right answer to the following?
    public interface Syrupable {
        void getSugary();
    }
    abstract class Pancake implements Syrupable {}

    class BuleBerryPancake implements Pancake {
        public void getSugary() { ; }
    }
    class SourdoughBuleBerryPancake extends BuleBerryPancake {
        void getSugary(int s) { ; }
    }
Answer: Compilation fails due to an error on line 6
  1. Find the Output
    try {
        Float f = new Float("3.0");
        int x = f.intValue();
        System.out.println("x : "+x);
        byte b = f.byteValue();
        System.out.println("b : "+b);
        double d = f.doubleValue();
        System.out.println("d : "+d);
        System.out.println(x + b + d);
    } 
    catch (NumberFormatException e) /* Line 9 */
    {
        System.out.println("bad number"); /* Line 11 */
    }
Answer: 9.0
  1. What is a covariant return type?
Answer: The overiding method can have base type as the return type instead of the derived type
Labels : #hackerrank certification ,#java (basic) ,

Post a Comment