Sample Questions

  1. What is the value of args[1]? (Select 1)
    public class Test {
       public static void main(String[] args) {
           ....
       }
    }
    >java Test This is a test
    
    a. "This"
    b. "is"
    c. "a"
    d. "test"
    
    
  2. Which of the following are legal methods?(Select 2)
    a. public null aMethod();
    b. public void aMethod();
    c. public aMethod();
    d. public Pet aMethod();
    
    
  3. Which of the following are legal statements? (Select 2)
    a. String s = (String)100;
    b. Float f = 1.2f;
    c. double d = 0;
    d. String str = "s";
    
  4. A reference type or an object can never be casted to a primitive type.
    a. True
    b. False
    
  5. Which of the following are correct about default constructor?(Select 2)
    a. Default constructor is a constructor that takes no arguments
    b. Default constructor is a constructor that takes only one argument
    c. Default constructor is a constructor existing only in your super class
    d. If you define no constructors at all in a class, then the compiler provides
       a default constructor.
    
    
  6. What are the correct description about class field?(Select 2)
    a. A class field is declared with static modifier
    b. A class field is declared with non-static modifier
    c. A class field is declared without any modifier
    d. A class field is associated with the class in which it is defined.
    
    
  7. Which of the following is a correct way to declare a constant? (Select 1)
    a. public final double PI = 3.1415926;
    b. public final PI = 3.1415926;
    c. public double PI = 3.1415926;
    d. int PI = (int)3.1415926;
    
    
  8. Given the code below:
    public class MyCounter {
        int counter;
        
        public static void main(String[] args) {
            counter++;
            System.out.println("counter: " + counter);
        } 
    }
    
    What will happen when you attemp to compile and run it?
    
    a. You get a compile-time error.
    b. You get printout "counter: 1" on the screen.
    c. You get printout "counter: 0" on the screen.
    d. You get no printout on the screen.
    
    
  9. Reusing the same method name with different arguments and perhaps a different return type is known as overloading.
    a. True
    b. False
    
  10. Which of the following are short circuit operators?
    a. &
    b. &&
    c. ||
    d. |
    
  11. What will happen when you attempt to compile the following code?(Select 1)
    class Bar {
       public static void main(String[] args) {
           String ch = args[0];
           switch (ch) {
              case 1:
                System.out.println("You got 1");
                break;
              case 2:
                System.out.println("You got 2");
                break;
              default:
                System.out.println("Wrong data");
           }
       }
    }
    a. You compile and run successfully
    b. You will get compile-time error because incompatible type.
    c. If you pass message 1, the printout is "You got 1".
    d. If you pass message 2, the printout is "You got 2".
    
    
  12. A class that is abstract may not be instantiated.
    a. True.
    b. False.
    
  13. The default layout manager for a Frame is ...
    a. FlowLayout 
    b. BorderLayout 
    c. GridLayout 
    d. GridBagLayout 
    e. CardLayout 
    
  14. Which of the following are valid adapter classes in Java for WindowListener interface. (Select 1)
    a. ClosingAdapter 
    b. ActionAdapter 
    c. ActivateAdapter 
    d. ItemAdapter 
    e. WindowAdapter 
    
  15. Which of the following are legal array declarations?(Select 2).
    a. int i[5][]; 
    b. int i[][]; 
    c. int []i[1]; 
    d. int i[5][5]; 
    e. int[][] a; 
    
  16. Which of the following are legal identifier names in Java? (Select 1)
    a. %abcd 
    b. 5abcd 
    c. 1abcd 
    d. package 
    e. _a_long_name 
    
  17. How many constructors in the following class Sun?
    class Sun extends Star { 
       public Sun() { 
           System.out.println("Star Wars1"); 
       } 
       public Sun(String v1) { 
           super(v1, "Saturn"); 
           System.out.println("Star Wars2"); 
       } 
       public Sun(String v2, String v3) { 
           if ( v2.substring(0, v2.length()).length() > v3.length()) 
              System.out.println("Mission to Mars"); 
           else 
              System.out.println("Earth"); 
        } 
    } 
    a. 0
    b. 1
    c. 2
    d. 3
    
  18. A class can only extend one other class.
    a. True
    b. False 
    
  19. Fields define state, methods define function or change, and constructors define how an object should be built as an instance of the class.
    a. True
    b. False
    
  20. If you create a subclass of Frame, the subclass inherits methods from Frame and any class that Frame inherits from.
    a. True
    b. False
    
  21. If you want to use setBackground() method in the Frame class and your class extends Frame, do you need to instantiate the Frame class and use dot to access its method setBackground()?
    a. NO. By extending to the Frame class, there is no need to instantiate Frame 
    in your class to get to its methods.  
    b. YES.
    
  22. What is the Panel? (Select 1)
    a. Panel is not a class predefined by SUN.
    b. Panel is the super class of Frame.
    c. Panel is the super class of Object.
    d. Panel is a container that can hold other components, containers, and has a layout manager associated with it.
    
  23. The border layout manager allows a maximum of five objects added to a panel. You can increase the number of objects added by adding another panel to each region.
    a. True
    b. False
    
  24. The regions of the BorderLayout class are written with all uppercase characters... like add(btn1, BorderLayout.NORTH) (Select 1)
    a. because these fields are static
    b. because these fields are constants and conform to naming conventions. 
       Constants are written in all uppercase letters.
    c. because these fields are non-static
    d. because these fields are public
    
  25. What is the purpose of the method XXX ? (Select 1)
    public int XXX(int a, int b) {
        if ( a > b)
           return a;
        else
           return b;
    }
    
    a. Return a value
    b. Find a larger value, and its name is max
    c. Find a smaller value, and its name is min
    d. None of the above.
    

Answers:

  1. Correct answer is b. args[0] is "This", args[1] is "is", args[2] is...
  2. Correct answers are b and d. A method must have a return type. If there is nothing to return use void, not null. Pet is an object, so d is a correct answer.
  3. Correct answers are c and d. A primitive type can never be casted to a reference type.
  4. Correct answer is a.
  5. Correct answers are a and d.
  6. Correct answers are a and d.
  7. Correct answer is a.
  8. Correct answer is a. Non-static variable cannot be accessed by static method.
  9. Correct answer is a.
  10. Correct answers are b and c.
  11. Correct answer is b. ch should be int type.
  12. Correct answer is a.
  13. Correct answer is b.
  14. Correct answer is e.
  15. Correct answers are b and e.
  16. Correct answer is e.
  17. Correct answer is d.
  18. Correct answer is a.
  19. Correct answer is a.
  20. Correct answer is a. Frame inherits the predefined methods from the classes Window, Container, Component, and Object.
  21. Correct answer is a.
  22. Correct answer is d.
  23. Correct answer is a.
  24. Correct answer is b.
  25. Correct answer is b.