Java
Java
Output :
inside parents's default constructor
inside child's default constructor
inside parent's argument constructor
inside child's argument constructor
Output:
Line 9, will give a compilation error. It is a recursive call to
the default constructor. However, it is legitimate to call it on
line 14 because it is being called from an argument
constructor. If we remove line 9, then the output will be :
Ans:
1. By passing the parameters 13 and 0 to the method
print, the statement nr = accounts[i] / i2; causes an
ArrayIndexOutOfBoundsException. The reason is that
element 13 doesn't exist. The equation nr =
accounts[i] / i2; first tries to access element 13, then
divides the number by zero. The code doesn't handle
ArrayIndexOutOfBoundsExceptions, but the Exception
is a generic Exception handler. The statement
System.out.print("T"); writes T to the standard output.
2. By passing the parameters 12 and 0 to the method
print, the statement nr = accounts[i] / i2; divides zero
by zero, which causes an ArithmeticException. The
statement System.out.print("S"); writes S to the
standard output. The correct answer is: D.
What happens when the following program is compiled and
run?
public class MyClass {
int y = 3;
public MyClass(int i) {
y += i;
}
public MyClass(int i, int i2) {
y += (i + i2);
System.out.print(y);
}
public int method(int i) {
y += i;
return y;
}
public static void main(String[] args) {
new MyClass(new MyClass(5).method(2), 4);
}
}
Ans:
1. The statement new MyClass(5).method(2) creates a
new object by passing the one-argument constructor of
the class MyClass.
2. By passing 5 to the constructor, the statement y += i;
increments the value of y by 5. So y = 3 + 5 = 8.
3. By invoking the method and passing the value 2 to it,
the statement y+= i; increments the value of y by 2. So,
y = 8 + 2 = 10.
4. The statement new MyClass(new
MyClass(5).method(2), 4); creates a new object by
calling the two-argument constructor.
5. The method returns the value 10. So, the statement
new MyClass(new MyClass(5).method(2), 4); is
equivalent to new MyClass(10, 4);
6. The y += (i + i2); increments the value of y by 10 + 4 =
3 + 10 + 4 = 17.
The correct answer is: d.
If the following code is compiled and run, it writes nothing
to the standard output.
Write only one statement at line 29. The statement should
apply the following.
1. The statement creates the object mc from the class
MyClass.
2. The statement should assign the value y to the object
str, assign the value 9 to the variable i, and the value 8
to the variable i2.
3. As a result of adding the statement, the output of the
code should become y98.
What is that statement?
public class MyClass
{
static String str = "x";
static int i = 2;
static int i2;
Ans:
1. We actually need to invoke the method intMethod to
assign a value to the variable i and invoke the
strMethod to assign a value to the object str.
2. By instantiating the class MyClass, we can create an
object. At the same time, we assign a value to the
variable i2.
3. By creating objects, the statement System.out.print(i2);
writes the value of i2 to the standard output. That is
unwanted in some cases. Therefore, we invoke the
methods as follows:
MyClass mc = new MyClass(new MyClass("", 0, 3).strMethod("y"), new
MyClass("", 0, 3).intMethod(9), 8);
Ans: