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 >>


20 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. then the answer is e none of the above

      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
    Replies
    1. it should be (b). You need this.value = i to assign i to the object.

      Delete
  6. I surprised with the analysis you made to make this particular publish incredible. Wonderful activity! 0 E 4thTaylor, TX 76574 MLS # 2178510

    ReplyDelete
  7. Are you tired of seeking loans and Mortgages,have you been turned down constantly By your banks and other financial institutions,We offer any form of loan to individuals and corporate bodies at low interest rate.If you are interested in taking a loan,feel free to contact us today,we promise to offer you the best services ever.Just give us a try,because a trial will convince you.What are your Financial needs?Do you need a business loan?Do you need a personal loan?Do you want to buy a car?Do you want to refinance?Do you need a mortgage loan?Do you need a huge capital to start off your business proposal or expansion? Have you lost hope and you think there is no way out, and your financial burdens still persists? Contact us (gaincreditloan1@gmail.com)

    Your Name:...............
    Your Country:...............
    Your Occupation:...............
    Loan Amount Needed:...............
    Loan Duration...............
    Monthly Income:...............
    Your Telephone Number:.....................
    Business Plan/Use Of Your Loan:...............
    Contact Us At : gaincreditloan1@gmail.com
    Phone number :+44-75967-81743 (WhatsApp Only)

    ReplyDelete
  8. will you explain question no.12 that how it works .
    class Color{
    static String blue="Color.blue";
    static String red="Color.red";
    }
    class Rectangle{
    String color;
    public void setColor(String color){
    this.color=color;
    System.out.println(color);
    }
    public static void main(String[] args) {
    Rectangle r=new Rectangle();
    r.setColor(Color.blue);
    Rectangle r2=r;
    r2.setColor(Color.red);

    }
    }
    output:
    Color.blue
    Color.red

    ReplyDelete
  9. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. java training in Chennai|| j2ee training in Chennai

    ReplyDelete
  10. Welcome to Joy Fredickson loan Organization, we are formed to help people in needs of helps,such as financial help. So if you are going through financial difficulty or you are in any financial mess,and you need funds to start up your own business or you need loan to settle your debt or pay off your bills,start a nice business, or you are finding it hard to obtain capital loan from local banks,contact us today via email: joyfredickson55@yahoo.com or WhatsApp number: +2348078800681
    INFORMATION NEEDED.
    Your Name...
    Last name...
    SEX..
    AGE...
    Country......
    PURPOSE OF LOAN........
    YOUR MONTHLY INCOME....
    Loan Amount needed....
    Loan Duration....
    Cell Phone Number....

    Thank you.

    ReplyDelete
  11. This blog is very useful for me and thanks for sharing
    Java Online Training Hyderabad

    ReplyDelete
  12. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating.
    oracle training in chennai|| hadoop training in chennai

    ReplyDelete
  13. I found your this post while searching for information about blog-related research ... It's a good post .. keep posting and updating information.
    Ghana passport online

    ReplyDelete
  14. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    Regards,
    hadoop training in chennai || selenium training in chennai




    ReplyDelete