Core Java Test

1. What is output of below code?
public class Test {
    public static int x = 7;
    public int y = 3;

    public static void main(String[] args) {
        Test a = new Test();
        Test b = new Test();
        a.y = 5;
        b.y = 6;
        a.x = 1;
        b.x = 2;
        System.out.print(a.y + " " + b.y + " " + a.x + " " + b.x + " ");
        System.out.print(Test.x);
    }
}
(a) What are the class variables?
A. Both x and y
B. Only x
C. Only y
D. Both a and b



(b) What are the instance variables?
A. Both x and y
B. Only x
C. Only y
D. Both a and b



(c) What is the output of above code?
A. 5 6 1 2 7
B. 5 6 1 2 2
C. 5 6 1 2 2
D. 5 6 2 2 2



2. What is output of bellow code?
public class Test {
    public static void main(String[] args) {
        String s = "foo";
        Object o = (Object) s;
        if (s.equals(o)) {
            System.out.print("AAA" + " ");
        } else {
            System.out.print("BBB" + " ");
        }
        if (o.equals(s)) {
            System.out.print("CCC" + " ");
        } else {
            System.out.print("DDD" + " ");
        }
    }
}
A. AAA DDD
B. BBB DDD
C. AAA CCC
D. BBB CCC



3. What is output of bellow code?
public class Test {
    public static void main(String[] args) {
        String x = "xyz";
        x.toUpperCase();
        String y = x.replace('Y''y');
        y = y + "abc";
        System.out.println(y);
    }
}
A. xYzabc
B. XyZabc
C. XYZabc
D. xyzabc



4. What is output of bellow code?
public class Test {
    public static void main(String[] args) {
        int i = (int) Math.random();
        System.out.println(i);
    }
}
A. Any Random number
B. 1
C. Compiler Error
D. 0



5. What is output of bellow code?
class A {
    public A(int x) {
    }
}

class B extends A {
}

public class Test {
    public static void main(String args[]) {
        A a = new B();
        System.out.println("complete");
    }
}
A. Compiler Error
B. complete
C. Runtime Error



6. What is output of bellow code?
public class Test {
    public static void main(String[] args) {
        double value = -9.0;
        System.out.println(Math.sqrt(value));
    }
}
A. Compiler Error
B. +3.0
C. -3.0
D. NaN



7. What is output of bellow code?
public class Test {
    public static void main(String[] args) {
        String s = "42";
        try {
            s = s.concat(".5");
            double d = Double.parseDouble(s);
            s = Double.toString(d);
            int x = (int) Math.ceil(Double.valueOf(s));
            System.out.println(x);
        } catch (NumberFormatException e) {
            System.out.println("Bad Number");
        }
    }
}
A. 43
B. 42
C. 43.5
D. Bad Number



8. What is output of bellow code?
interface K {
    int k = 5;
}

public class Test implements K {
    public static void main(String args[]) {
        int i;
        Test test = new Test();
        i = test.k;
        i = Test.k;
        i = K.k;
        System.out.println(i);
    }
}
A. Compiler Error
B. Runtime Exception
C. 5
D. 5.0



9. What is output of bellow code?
public class Test {
    public static void main(String[] args) {
        String a = "newspaper";
        a = a.substring(5, 7);
        char b = a.charAt(1);
        a = a + b;
        System.out.println(a);
    }
}
A. apep
B. app
C. apea
D. appp



10. What is output of bellow code?
public class Test {
    public static void main(String[] args) {
        String s1 = "abc";
        String s2 = "def";
        String s3 = s2;
        s2 = "ghi";
        System.out.println(s1 + " " + s2 + " " + s3);
    }
}
A. abc ghi ghi
B. abc def def
C. abc ghi def
D. ghi def abc



11. What is output of bellow code?
public class Test {
    public static void stringReplace(String text) {
        text = text.replace('j''c');
    }

    public static void bufferReplace(StringBuffer text) {
        text = text.append("c");
    }

    public static void main(String args[]) {
        String textString = new String("java");
        StringBuffer textBuffer = new StringBuffer("java");
        stringReplace(textString);
        bufferReplace(textBuffer);
        System.out.println(textString + " " + textBuffer);
    }
}
A. java javac
B. cava javac
C. cava java
D. java java



12. What is output of bellow code?
class Tree {
}

class Pine extends Tree {
}

class Oak extends Tree {
}

public class Forest {
    public static void main(String[] args) {
        Tree tree = new Oak();
        if (tree instanceof Pine)
            System.out.println("Pine");
        else if (tree instanceof Tree)
            System.out.println("Tree");
        else if (tree instanceof Oak)
            System.out.println("Oak");
        else
            System.out.println("Oops ");
    }
}
A. Tree
B. Pine
C. Oak
D. Oops



13. What is output of bellow code?
public class Test {
    public static void main (String [] args){
           String d = "bookkeeper";
           d.substring(1,7);
           d = "w" + d;
           d.append("woo");
           System.out.println(d);
    }
}
A. Compiler Error
B. wookkeewoo
C. wbookkeeperwoo
D. Runtime Error



14. What is output of bellow code?
public class Test {
    public static void main(String[] args) {
        String a = "ABCD";
        String b = a.toLowerCase();
        b.replace('a''d');
        b.replace('b''c');
        System.out.println(b);
    }
}
A. dbcd
B. dccd
C. accd
D. abcd



15. What is output of below code?
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello");
        main(args);
    }
}
A. Compiler Error
B. StackOverflowError Exception
C. Hello
D. Display Hello Infinite Times



16. What is output of below code?
public class Test {
    public static void main(String[] args) {
        StringBuffer s = new StringBuffer("Hello");
        System.out.print(s.length() + " ");
        System.out.print(s.capacity());
    }
}
A. 5 5
B. 5 10
C. 5 16
D. 5 21



17. What is output of bellow code?
public class Test {
    public static void main(String[] args) {
        String s1 = "Hello";
        String s2 = new String("Hello");
        s2 = s2.intern();
        System.out.println(s1 == s2 ? "s1 == s2" : "s1 != s2");
    }
}
A. s1 == s2
B. s1 != s2
C. Compiler Error
D. Runtime Error



18. What is output of bellow code?
public class Test {
    public static void main(String[] args) {
        String str = "Hello";
        System.out.print(str.substring(2) + " ");
        System.out.print(str.substring(2, 4));
    }
}
A. llo ll
B. llo llo
C. Hello Hello
D. ello ll



19. What is output of bellow code?
public class Test {
    public static void main (String [] args){
              String str = "9876567342",str1 = "I am from Bangalore Karnataka";
              System.out.print(str.matches("[\\d]{8,12}") + " ");
              System.out.print(str1.startsWith("am") + " ");
              System.out.print(str1.endsWith("taka") + " ");
              System.out.print(str1.contains("galor") + " ");
       }
}
A. false false true true
B. true false true true
C. true false true false
D. false false false false



No comments:

Post a Comment