Consider the below given code fragment.
Rectangle r1
new Rectangle () :
r1.setColor (Color.blue)
Rectangle r2 = 11;
r2.setColor (Color.red)
Answers
Answer:
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
Answer:
The Correct answer is option C
Explanation:
Rectangle r1
new Rectangle () :
r1.setColor (Color.blue)
Rectangle r2 = 11;
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.
Both r1 and r2 are referring the same object of Rectangle class. So, finally the Color of the object is changed to red.
Program
public class MyClass {
public static void main(String args[]) {
Rectangle r1 = new Rectangle();
r1.setColor(Color.RED);
Rectangle r2 = r1;
r2.setColor(Color.BLUE);
System.out.println(r1.getColor());
System.out.println(r2.getColor());
}
static enum Color {
RED, BLUE
}
static class Rectangle {
private Color color;
public void setColor(Color color) {
this.color = color;
}
public Color getColor() {
return this.color;
}
}
}
Reference Link
https://brainly.in/question/29236360