Friday 28 August 2015

Java tricky interview question.

source : http://way2java.com/java-questions/test-your-java-2/
source : http://www.javabeat.net/ocajp-mock-exam-questions-java-basics/


Test Your Java 2


Total 8 tests are available under "Test Your Java" series. Test-1Test-2Test-3Test-4Test-5Test-6 are basic level questions and Test-7, Test-8 are advanced questions. Answers are given for each question at the end of each test. Answering these questions increases your knowledge in Java and helps you a lot when you appear for interviews. Solving these basic questions is your first step for SCJP preparation.
    1.
  1.      class Numbers   
         {
           public int display(int x, int y)   
           {
      return ("The sum of x and y is " + x+y);
           }
           public static void main(String args[])  
           {
             Numbers num = new Numbers();
      System.out.println(num.display(4,5));
           }   
          }
    What is the output of the above program ?
    a) The sum of x and y is 9 b) The sum of x and y is 45 c) does not compile d) none
  2. 2.
  3.      class  WhatOutput   
         {
           public void display(int x, double y)
           {   
             System.out.println(x+y);           
           }
           public double display(int p, double q)   
           {   
             return (p+q);   
           }
           public static void main(String StringArray[])  
           {
      WhatOutput wo = new WhatOutput();
      wo.display(4, 5.0);    
             System.out.println(wo.display(4, 5.0));
           }   
         }
    The output of the above program is 9.0 and 9.0.
    a) True b) False
  4. 3.
  5.      class staticMethods
         {  
           static void display()   
           {  
             System.out.println("display");  
           }
           static  
           {  
             System.out.println("only static no method");   
           }
           public static void main(String s[])   
           {
      display();
           }    
         }
    What is the output of the above program ?
    a) display b) only static no method and display c) display and only static no method d) does not compile due to error in third line e) none
  6. Overloaded methods must have the same return types.
    a) True b) False
  7. 5.
  8.      public class Room   
         {
           public static void main(String args[])  
           {
             int  height=10 ,  width=10,  length=20;
      System.out.println("Volume is " + (width*height*length));
           }     
         }
    The above program creates an object by name Room and displays its volume as 2000.
    a) True b) False
  9. The implicit return type of a constructor is
    a) void b) depends upon the called method c) a class object in which it is defined d) none
  10. 7.
  11.      class Numbers   
         {
           public static void main(String args[])
           {   
             int a=20, b=10;
             if((a < b) && (b++ < 25))
                System.out.println("This is any language logic");
             System.out.println(b);
           }   
         }
    What is the output of the above program ?
    a) 12 b) 11 c) 10 d) program does not compile e) throws exception f) none
  12. Both switch and if tests for boolean type.
    a) True b) False
  13. In Java every method need not be associated with an object.
    a) True b) False
  14. The finalize() method is called just prior to
    a) an object, variable or method goes out of scope.
    b) an object or variable goes out of scope.
    c) a variable goes out of scope d) before garbage collection e) none
  15. All classes in a source file should contain main method.
    a) True b) False
  16. The main method should be static for the reason
    a) it can be accessed easily by the class loader.
    b) it can be accessed by every method or variable without any hindrance.
    c) it can be executed without creating any instance of the class. d) none
  17. 13.
  18.      class Weather   
         {
           static boolean isRaining;
           public static void main(String args[])  
           {
             System.out.print(isRaining);
           }    
          }
    The above program
    a) prints true b) prints false c) does not compile as boolean is not initialized d) does not compile boolean can never be static e) c and d f) none
  19. 14.
  20.      int i;
         for(i = 1; i < 6; i++)    
         {
           if (i > 3)  
             continue ;
         }
         System.out.println(i);
    What is the output of the above fraction of code ?
    a) 2 b) 3 c) 4 d) 5 e) 6 f) does not compile g) none
  21. Constructors and methods of a class can be inherited.
    a) True b) False
  22. Float f1 = new Float("3.2");
    float f = f1.floatValue();
    System.out.println(f);
    The above statements raises a compilation error as 3.2 is not mentioned as 3.2f.
    State a) True b) False
  23. I can instantiate a class which implement an interface.
    a) True b) False
  24. An abstract class should have methods all declared abstract.
    a) True b) False
  25. An interface can be extended from another interface.
    a) True b) False
  26. 20.
  27.      class XXX   
         {
           void show()  
           {   
             System.out.println("XXX");   
           }
         }
         class YYY extends XXX   
         {
           void show()  
           {
             super();
      System.out.println("YYY") ;
           }
           public static void main(String args[])  
           {
      new YYY().show();
           }
         }
    What is the output of the above program ?
    a) XXX b) YYY c) a and b d) does not compile e) throws exception f) none
  28. 21.
  29.      public  static void main(String args[])  
         {
           public int firstNumber = 1;   
           private double secondNumber = 1.0;
           System.out.println(firstNumber + secondNumber);
         }
    The above code prints 2.0. State a) true b) false
  30. int first /* house */ Number = 1;
    System.out.println(firstNumber);
    The above code prints 1. State a) true b) false
  31. 23.
  32.      switch(1)   
         {
           case 1: System.out.println("SNRao"); 
              break;
           case 1: System.out.println("Sridhar");     
              break;
         }
    Writing same case statement two times is a compilation error.
    State a) true b) false
  33. 24.
  34.      class ForLoop  
         {
           public static void main(String args[])  
           {
             for(int i = 0, int j = 0; i < 3; i++, j++)
        System.out.println(i + j);
           }    
         }
    What is the output of the above program ?
    a) 0, 2, 4 b) 0, 1, 2 c) does not compile d) throws exception e) none
  35. 25.
  36.      class  GuessWhat   
         {
           public static void  main(String args[])  
           {
             int min = 0;
      min(10, 20, min);
      System.out.println(min);
           }
           public static void min(int number1, int number2, int min)  
           {
      if(number1 > number2)
        min = number1;
      else
        min = number2;
            }   
           }
    What is the output of the above program ?
    a) 0 b) 10 c) 20 d) does not compile as the compiler can not differentiate between min variable and min method e) throws exception due to min variable in min method f) none

ANSWERS

1. c2. b3. b4. b5. b
6. c7. c8. b9. a10. d
11. b12. c13. b14. e15. b
16. b17. a18. b19. a20. d
21. b22. b23. a24. c25. a
- See more at: http://way2java.com/java-questions/test-your-java-2/#sthash.DyjAyVbd.dpuf

No comments:

Post a Comment