Java Aptitude

Java OCJP

OCJP Practice MCQ 1

  1. Read the below code snippet:

    
    
    public class Q1 {
         //line2
          public static void main(String[] args) {
         //line4           
            System.out.println("The value of ch: " + ch);
          }
    }

    Which of these following given statement allow this program to compile and execute?

    1."char ch;" placed on line 2
    2."char ch;" placed on line 4
    3."char ch = 'g';" placed on line 2
    4."char ch = 'g';" placed on line 4
    5."static char ch='g';" placed on line 2
    6.
    "char ch = new char();" placed on line 4

     

     

    A.

    B.

    C.

    D.

  2. Which of the following are correct in java?
    1. java final keyword can be used with variables, methods and class.
    2. You can create only one instance of abstract class.
    3. interface extends interface.
    4. List, Set, Map are classes in java.

    A.

    B.

    C.

    D.

  3. Which of the following is not correct?
    1. Java is a platform.
    2. In interface all the variables are by default public, static, final.
    3. You can create thread only by extending the Thread class.
    4. Java is platform dependent language.

    A.

    B.

    C.

    D.

  4. Which statement is correct in java?
    1. Java provides default constructor in case programmer doesn't create one.
    2. Constructor can be overloaded.
    3. Constructor can be overridden.

    A.

    B.

    C.

    D.

  5. Which of these statements is not a legal way to declare array in java?

    A.

    B.

    C.

    D.

  6. What can directly access and manipulate the value of the variable "roomNo

    package com.tih;
    
    public class Hostel  {
        public int roomNo = 107;
    }
    
    
    	

     

    A.

    B.

    C.

    D.

  7. How can you forcefully call garbage collection of an object?

    A.

    B.

    C.

    D.

  8. Guess output of below code snippet:

    
    
    class Hello {
     int add(int i, int j) {    //line 2
      return i + j;
     }
    }
    
    public class HelloWorld extends Hello {
     public static void main(String argv[]) {
      short sNum = 10;
      System.out.println(add(sNum, 6));  //line 10
     }
    }
    

    A.

    B.

    C.

    D.

  9. Given below is code written in same source file. Guess the output

    
    
    public class Hello {
    
     static int add(int i, int j) { //line 2
    
      return i + j;
    
     }
    
    }
    
    
    
    public class HelloWorld extends Hello {
    
     public static void main(String argv[]) {
    
      short sNum = 10;
    
      System.out.println(add(sNum, 6));  //line 10
    
     }
    
    }

     

    A.

    B.

    C.

    D.

  10. Given code is saved in source file "HelloWorld.java"

    public class HelloWorld {
     boolean istrue;  //line 2
     static int p;    //line 3 
    
     public void printValue() {
      System.out.print(istrue);
      System.out.print(p);  //line 7
     }
    }
    
    class Test {
     public static void main(String argv[]) {
     HelloWorld hw = new HelloWorld();
     hw.printValue();
     }
    }
    
    

    Guess the output?

    A.

    B.

    C.

    D.


Please Share this page