Skip to main content

IS5103 Week 6 (Quiz)

 Quiz


1. Analyze the following code:

public class Test extends A {
public static void main(String[] args) {
Test t = new Test();
t.print();
}
}

class A {
String s;

A(String s) {
this.s = s;
}

public void print() {
System.out.println(s);
}
}

A). 1. The program would compile if a default constructor A(){ } is added to class A explicitly.

    2. The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed.


2. What is the output of running class C?

class A {
public A() {
System.out.println(
"The default constructor of A is invoked");
}
}

class B extends A {
public B() {
System.out.println(
"The default constructor of B is invoked");
}
}

public class C {
public static void main(String[] args) {
B b = new B();
}
}

A). "The default constructor of A is invoked" followed by "The default constructor of B is invoked"

 

3). Analyze the following code:

public class Test {
public static void main(String[] args) {
B b = new B();
b.m(5);
System.out.println("i is " + b.i);
}
}

class A {
int i;

public void m(int i) {
this.i = i;
}
}

class B extends A {
public void m(String s) {
}
}


A). The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.


4. What is the output of the following code?

public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}

class Student extends Person {
@Override
public String getInfo() {
return "Student";
}
}

class Person {
public String getInfo() {
return "Person";
}

public void printPerson() {
System.out.println(getInfo());
}
}


A). Person Student


5. Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ________.


A). true


6. Analyze the following code

// Program 1:
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}

class A {
int x;

public boolean equals(A a) {
return this.x == a.x;
}
}


// Program 2:
public class Test {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
}
}

class A {
int x;

public boolean equals(A a) {
return this.x == a.x;
}
}


A)Program 1 displays false and Program 2 displays true.


7. For the questions below, consider the following class definition:

public class AClass
{

protected int x;

 

protected int y;

 

public AClass(int a, int b)

 

{

 

x = a;

 

y = b;

 

}

 

public int addEmegg

 

{

 

return x + y;

 

}

 

public void changeEmegg

 

{

 

x++;

 

y--;

 

}

 

public String toStringegg

 

{

 

return "" + x + " " + y;

 

}

}

A). Redefine addEm to return the value of z + super.addEmegg, but leave changeEm alone



8). For the questions below, use the following partial class definitions:

public class A1
{

public int x;

 

private int y;

 

protected int z;

...
}

public class A2 extends A1
{

protected int a;

 

private int b;

...
}

public class A3 extends A2
{

private int q;

...
}

A). x, z, a, b


9. If class AParentClass has a protected instance data x, and AChildClass is a derived class of AParentClass, then AChildClass can access x but can not redefine x to be a different type.

A). False


10. If classes C1 and C2 both implement an interface Cint, which has a method whichIsIt, and if C1 c = new C1 ; is performed at one point of the program, then a later instruction c.whichIsIt ; will invoke the whichIsIt method defined in C1.


A).  False




















Comments