THINK FIRST·CODE LATER

← Java Programming
Chapter 11 · Week 4–5

Object References, Constructors, and Overloading

Answered 0/37 Correct 0
Sign in to save progress across devices
Q1

Suppose you are writing the definition of the default constructor of class MyClass. Which heading is correct?

Q2

Which statement correctly constructs a MyClass object called myObject?

Q3

Suppose greetings() is declared public static void in MyClass. Which call is correct from another class?

Q4

Given public void update(int num, String title) in MyClass and an object myObject, which call is correct?

Q5

Suppose you are writing the parameterised constructor of Employee. Which heading is correct?

Q6

What does the compiler do when a class declares no constructor at all?

Q7

A class declares only public Student(String name). What is the result of new Student() elsewhere?

Q8

Which statement about constructors is false?

Q9

Where must a call to this(...) appear inside a constructor?

Q10

What is the result of compiling and executing the following Java class?

package exam;
public class Question1 {
    static int start = 2;
    final int end;
    public Question1(int i) {
        i = 5;
        end = i;
    }
    public void fly(int distance) {
        System.out.print(end - start + " ");
        System.out.print(distance);
    }
    public static void main(String... start) {
        new Question1(2).fly(5);
    }
}
Q11

Which line of code, inserted at // number6, causes the application to print 5?

package exam;
public class Question3 {
    private int rope = 1;
    protected boolean outside;
    public Question3() {
        // number6
        outside = true;
    }
    public Question3(int rope) {
        this.rope = outside ? rope : rope + 1;
    }
    public static void main(String[] bounce) {
        System.out.print(new Question3().rope);
    }
}
Q12

What is the result of compiling and executing the following?

package exam;
public class Question2 {
    private static int yesterday = 1;
    int tomorrow = 10;
    public static void main(String[] hello) {          // line 5
        Question2 q = new Question2();
        int today = 20, tomorrow = 40;                 // line 7
        System.out.print(today + q.tomorrow + q.yesterday);
    }
}
Q13

What is the output?

public class Point {
    int x;
    public static void main(String[] args) {
        Point a = new Point();
        Point b = a;
        b.x = 9;
        System.out.print(a.x);
    }
}
Q14

Two objects created with identical field values are compared with ==. The result is:

Q15

Why does a.equals(b) return false for two distinct objects with identical data in a class that does not override equals?

Q16

To make equals compare contents, a class must:

Q17

What is the output?

public class Acc {
    private double balance;
    public void deposit(double d) { balance += d; }
    public double getBalance() { return balance; }
    static void bonus(Acc a) { a.deposit(100); }
    public static void main(String[] args) {
        Acc x = new Acc();
        x.deposit(50);
        bonus(x);
        System.out.print(x.getBalance());
    }
}
Q18

What is the output if bonus is changed to { a = new Acc(); a.deposit(100); }?

Q19

If s is an instance variable (a field), what does the declaration Student s; alone place in s?

Q20

Which of these is a valid overloaded pair of constructors?

Q21

What is the output?

public class Book {
    private String title;
    public Book() { this("Untitled"); }
    public Book(String t) { title = t; }
    public String getTitle() { return title; }
    public static void main(String[] args) {
        System.out.print(new Book().getTitle());
    }
}
Q22

In the previous class, how many constructors does Book have?

Q23

Which statement about public void Student() inside class Student is true?

Q24

What is the output?

public class Chain {
    private int v;
    public Chain set(int v) { this.v = v; return this; }
    public int get() { return v; }
    public static void main(String[] args) {
        System.out.print(new Chain().set(3).set(7).get());
    }
}
Q25

What makes the chaining in the previous question possible?

Q26

What happens when a chained call such as a.getB().getName() is evaluated and getB() returns null?

Q27

Which statement creates two independent objects?

Q28

After Student a = new Student(); a = null;, the object originally created is:

Q29

Which is stored on the heap?

Q30

What is the output?

public class Counter {
    private int n;
    public Counter(int n) { this.n = n; }
    public void inc() { n++; }
    public int get() { return n; }
    public static void main(String[] args) {
        Counter a = new Counter(5);
        Counter b = new Counter(5);
        a.inc();
        System.out.print(a.get() + " " + b.get() + " " + (a == b));
    }
}
Q31

A constructor that takes no parameters and is written by the programmer is called:

Q32

Which of these is not a good reason to write several constructors for one class?

Q33

Given public Employee(String name, double salary) in Employee, which call compiles?

Q34 Short answer

Draw the memory picture (stack and heap) after these three lines and say how many objects exist.

Student a = new Student("Ali", 60);
Student b = new Student("Ali", 60);
Student c = a;
Q35 Short answer

Explain the difference between a == b and a.equals(b), and state precisely what a class must do so that the second one reports equal contents.

Q36 Short answer

A student adds public Rectangle(double w, double h) to a class and the program suddenly fails to compile at a line new Rectangle() that worked yesterday. Explain what happened and give two ways to fix it.

Q37 Programming

Write a class Rectangle with private width and height; a no-argument constructor that sets both to 1; a parameterised constructor that delegates the validation to a private helper (negative values become 1); accessors, mutators, getArea(), getPerimeter(); a boolean isSquare(); and an overridden equals that treats two rectangles with the same dimensions as equal. Write a driver that demonstrates every one of these, including the difference between == and equals.