Quiz 1 Solutions
Question 1
If two variables contain references to the same object, then…
- the object may be modified using either reference
- the object cannot be modified unless there is only one reference to it
- third reference is created when the object is modified
- none of the above
Question 2
A variable declared within a method is known as a…
- static variable
- instance variable
- parameter
- local variable
Question 3
A local variable can be declared within…
- the parameter list of a method
- the body of a method
- a block within the body of a method
- all of the above
Question 4
A valid argument to the System.out.println method are…
- “anything with double quotes”
- a variable of type int
- a concatenation of multiple strings
- all of the above
Question 5
The hardest kind of errors to detect in a computer program are…
- Compilation error
- Logic error
- Runtime error
- Compilation error
Question 6
The bahaviour of an object is defined by its
- methods
- constructors
- instance variables
- all of the above
Question 7
In Java, null can be used…
- to indicate that a variable has no meaningful vale
- in a boolean expression with
==
- as an argument to a formal parameter
- all of the above
Question 8
The most appropriate syntax for a Java named constant
SALES_TAX
is…
- public double SALES_TAX = 13.50;
- public static final double SALES_TAX = 13.50;
- private static final double SALES_TAX = 13.50;
- public static double SALES_TAX = 13.50;
Question 9
The new operator
- allocated memory
- is used to create an object of a class
- is used with a constructor of a class
- all of the above
Question 10
In defining a Java class..
- a constructor has to be explicitly defined
- more than one constructors can be defined in needed
- exactly one constructor can be defined
- none of the above
Question 11
To define a class that represents a car, which of the following is the most appropriate?
- private class Car
- public class Car
- public class car
- public class CAR
Question 12
What happens if you define a constructor with a void return type?
- you will likely recieve a syntax error
- A default no-argument constructor will be used instead of the one you are defining
- There is nothing wrong with defining a constructor with a void return type
- The program will compile with a warning, but you will get a run-time error
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;
- double[]: specify a data type;
- new double[10]: creates an array of 10 double numbers;
- 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:
- Fixed memory with no structure
- Only 8 types available
- System-defined
Class types:
- Variable memory with a two-level structure
- Unlimited classes
- User-defined
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.
- Special initializations (e.g., String greeting = “Hello”;)
- Convenient concatenations (e.g, “Hello” + “ “ + “World”)
- 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.
- RATE
- hours
- minutes
- fee
Identify all variables that are visible from within an object of the class.
- RATE
- hours
- minutes
- fee
Identify all variables that are visible from within the computerFee method.
- RATE
- hours
- minutes
- fee
- hoursWorked
- minutesWorked
- quarterHours
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?
- public int sum(int a, int b) { return a + b; }
- public double sum(double a, double b) { return a + b; }
sum(2,3)
matches 1.sum(2.5,3.5)
matches 2.sum(2.5,3)
matches 2.
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));
}
}
- No, two methods with the same name.
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;
}