IS5103 Week 6 (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);
}
}

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();
}
}

 “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) {
}
}

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());
}
}

Person Student

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

 true

6. Analyze the following code

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

Program 1 displays false and Program 2 displays true.

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

public class AClass
{

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
{

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.

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.

False

Other Links:

Statistics Quiz

Networking Quiz

See other websites for quiz:

Check on QUIZLET

Check on CHEGG

Leave a Reply

Your email address will not be published. Required fields are marked *