Java Programming OOP Questions and Answers Set 2

Java Programming OOPs

Questions 11 to 20



11.
Among these expressions, which is(are) of type String?
(a)  "0"                                                   (b)  "ab" + "cd"                                      
(c)  '0' 
(d)  Both (A) and (B) above                    (e)  (A), (B) and (C) above.
12.
Consider the following code fragment
                  Rectangle r1 = new Rectangle();
                  r1.setColor(Color.blue);
                  Rectangle r2 = r1;
                  r2.setColor(Color.red);

             After the above piece of code is executed, what are the colors of r1 and  
             r2 (in this order)?  
(a)   Color.blue
       Color.red
(b)   Color.blue
       Color.blue
(c)   Color.red
       Color.red
(d)   Color.red
       Color.blue
(e)   None of the above.
13.
What is the type and value of the following expression? (Notice the integer division)
                   -4 + 1/2 + 2*-3 + 5.0
(a)  int -5                                                (b)  double -4.5                                      
(c)  int -4                             
(d)  double -5.0                                      (e)  None of the above.
14.
What is printed by the following statement?
System.out.print("Hello,\nworld!");
(a)  Hello, \nworld!   (b)  Hello, world!                                   
(c) 
(d)  "Hello, \nworld!" (e)  None of the above.
15.
Consider the two methods (within the same class)
         public static int foo(int a, String s)
         {
           s = "Yellow";
           a=a+2;
           return a;
         }
         public static void bar()
         {
           int a=3;
           String s = "Blue";
           a = foo(a,s);
           System.out.println("a="+a+" s="+s);
         }
         public static void main(String args[])
         {
            bar();
         }
What is printed on execution of these methods?
(a)  a = 3 s = Blue                                  (b)  a = 5 s = Yellow                                           (c)  a = 3 s = Yellow
(d) a = 5 s = Blue                                   (e) none of the above.
16.
Which of the following variable declaration would NOT compile in a java program?
(a)  int var;               (b)  int VAR;            (c)  int var1;             (d)  int var_1;           (e)  int 1_var;.
17.
Consider the following class definition:
            public class MyClass
            {
            private int value;
            public void setValue(int i){ /* code */ }
            // Other methods...
            }
The method setValue assigns the value of i to the instance field value. What could you write for the implementation of setValue?
(a)  value = i;                                         (b)  this.value = i;                                (c)  value == i;
(d)  Both (A) and (B) and above              (e)  (A), (B) and (C) above.
18.
Which of the following is TRUE?
(a)   In java, an instance field declared public generates a compilation error.
(b)   int is the name of a class available in the package java.lang
(c)   Instance variable names may only contain letters and digits.
(d)   A class has always a constructor (possibly automatically supplied by the java compiler).
(e)   The more comments in a program, the faster the program runs.
19.
A constructor
(a)   Must have the same name as the class it is declared within.
(b)   Is used to create objects.
(c)   May be declared private
(d)   Both (A) and (B) above
(e)   (a), (b) and (c) above.
20.
Consider,
              public class MyClass
              {
              public MyClass(){/*code*/}
               // more code...
               }
To instantiate MyClass, you would write?
(a)   MyClass mc = new MyClass();
(b)   MyClass mc = MyClass();
(c)   MyClass mc = MyClass;
(d)   MyClass mc = new MyClass;
(e)   The constructor of MyClass should be defined as, public void MyClass(){/*code*/}.



Answers



11.
Answer : (d)
Reason:  Strings are  "0" and "ab" + "cd" .
12.
Answer : (c)
Reason:  Both r1 and r2 are referring the same object of Rectangle class. So, finally the Color of the object is changed to red.
13.
Answer : (d)
Reason:  The execution goes on like this:
-4 + 1/2 + 2*-3 + 5.0;
-4 +  0 +  -6 +  5.0;  // integer division: 1/2 truncates .5
-10 +  5.0;  // higher type is double 5.0, so -10 is casted to double
-5.0;  //  finally, double -5.0.
14.
Answer : (c)
Reason:  The statement
System.out.print("Hello,\nworld!");
gives output as
15.
Answer : (d)
Reason:  ‘a’ value is returned from the method and so it is 5. But the string will remain same, as it is passed by value to the method.
16.
Answer : (e)
Reason:  The first character of a variable name should not be a digit.
17.
Answer : (d)
Reason:  ‘==’ is a comparison operator.
18.
Answer : (d)
Reason:  A class will always have a constructor, either provided by the user or a default constructor provided by the compiler.
19.
Answer : (e)
Reason:  A constructor
·                    Must have the same name as the class it is declared within.
·                    Is used to create objects.
·                    May be declared private.
20.
Answer : (a)
Reason:  An object is created by using a new operator.


<< Prev   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22  

 23   24   25   26   27   28   29   30  Next >>


8 comments :

  1. in 14th question ,Answer is None of these
    As Output coming is
    Hello,
    world!

    ReplyDelete
    Replies
    1. yes
      the answer (c) should to be that but not puted

      Delete
  2. yeah, i guessed it too. but i don't know why the answer is [blank]

    ReplyDelete
  3. In question No 19.
    How constructor is used to create object.
    constructor is used to initialize the state of the object created.

    ReplyDelete
  4. very interesting post.this is my first time here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post!
    java examples

    ReplyDelete
  5. can any buddy explain me question no 17

    ReplyDelete