Quiz 1 Solutions

Question 1

If two variables contain references to the same object, then…

Question 2

A variable declared within a method is known as a…

Question 3

A local variable can be declared within…

Question 4

A valid argument to the System.out.println method are…

Question 5

The hardest kind of errors to detect in a computer program are…

Question 6

The bahaviour of an object is defined by its

Question 7

In Java, null can be used…

Question 8

The most appropriate syntax for a Java named constant SALES_TAX is…

Question 9

The new operator

Question 10

In defining a Java class..

Question 11

To define a class that represents a car, which of the following is the most appropriate?

Question 12

What happens if you define a constructor with a void return type?

Question 13

In the following code fragment, square brackets [] appear three times. Briefly explain the meanings for each occurrence of these brackets.

double[] scores = new double[10];
for (int i = 0; i < scores.length; i++)
    scores[i] = i *100.0;
  1. double[]: specify a data type;
  2. new double[10]: creates an array of 10 double numbers;
  3. scores[i] accesses the indexed double number at location i.

Question 14

Java is a highly portable language. Briefly explain what makes it highly portable and what is the main disadvantage as a result of such a decision.

Java compiles a program into byte-code, which is machine-independent and can run on any machine with a JRE (Java Runtime Environment). The main disadvantage is that the byte-code is not as efficient compared to languages such as C that generates the machine-specific code.

Question 15

Java has two categories of data types: primitives and classes. List three major differences between the two categories.

Primitive types:

Class types:

Question 16

String is one of the most popular pre-defined classes in Java. As a result, Java gives it some special treatments compared to regular classes. List three such special treatments that make the use of strings convenient and safe.

  1. Special initializations (e.g., String greeting = “Hello”;)
  2. Convenient concatenations (e.g, “Hello” + “ “ + “World”)
  3. Read-only access (e.g., safer for parameter passing)

Question 17

Write a Java method named “average”, which takes an array of integers as a parameter, computes the average of these elements, and returns it to the calling method.


public double average(int[] numbers) {
    double sum = 0.0;
    for (int i = 0; i < numbers.length; i++)
        sum += numbers[i];
    return (sum/number.length);
}

Question 18

What is the output of the following Java statements:

Input:

String str = "Java Programming!";
System.out.println(str.equals("java programming!"));
System.out.println(str.equalsIgnoreCase("java programming!"));
System.out.println(str.toLowerCase());
System.out.println(str.substring(5));
System.out.println(str.substring(4, 11));

Output:

false
true
java programming!
Programming!
progra

Question 19

public class Bill {
    public static final double RATE = 150.0;
    private int hours;
    private int minutes;
    private double fee;
    ...

    public double computeFee(int hoursWorked, int minutesWorked) {
        minutesWorked = hoursWorked * 60 + minutesWorked;
        int quarterHours = minutesWorked / 15;
        return quarterHours * RATE;
    }
}

Identify all variables that are only visible from within the class.

Identify all variables that are visible from within an object of the class.

Identify all variables that are visible from within the computerFee method.

Question 20

public class Book {
    private String title;
    private String author;
    private int price;
    ...
}

Given a Book class with three instance variables as shown below, write an “equals” method that tests if two Book objects have the same content.

public boolean equals(Book other) {
    if (other == null)
        return false;
    else
        return title.equals(other.title) &&
        author.equals(other.author) &&
        price == other.price;
}

Question 21

For the calls “sum(2,3)”, “sum(2.5,3.5)”, and “sum(2.5,3)”, which one of the following two methods will be matched, respectively?

  1. public int sum(int a, int b) { return a + b; }
  2. public double sum(double a, double b) { return a + b; }

Question 21

Can the following code pass through the Java compiler? If not, which part is causing the problem and why?

public class Example {
    public double sum(int a, double b) { return a + b; }
    public double sum(double a, int b) { return a + b; }
    public static void main(String[] args) {
        Example obj = new Example();
        System.out.println(obj.sum(5, 8));
    }
}

Question 23

public class Book {
    private String title;
    private String author;
    private int price;
    ...
}

Write a “toString” method that shows the content of a book using the format: “Absolute Java by Walter Savitch at $120”, where “Absolute Java” is the title, “Walter Savitch” is the author, and 120 is the price for the book.

public String toString() {
    return title + " by " + author + " at $" + price;
}