How many copies of an instance variable exist when five objects of a class have been
created?
Q3
What does the operator new return?
Q4
In account.deposit(500), which is the calling object?
Q5
Inside an instance method, this refers to:
Q6
Which setter is written correctly?
Q7
Consider the following Java code. What will happen when you try to compile and run this
program?
public class BankAccount {
double balance;
void deposit(double amount) {
balance += amount;
System.out.println("Balance: " + balance);
}
}
public class Main {
public static void main(String[] args) {
BankAccount.deposit(300);
}
}
Q8
What is the smallest change that makes the previous program work?
Q9
What is the output?
public class Item {
private int count;
public void add() { count++; }
public int getCount() { return count; }
public static void main(String[] args) {
Item a = new Item();
Item b = new Item();
a.add(); a.add(); b.add();
System.out.print(a.getCount() + " " + b.getCount());
}
}
Q10
If the update method below is called, what can we say about the member variables
numOfItems and reportTitle?
public void update(int num, String title) {
int numOfItems = num;
reportTitle = title;
}
Q11
Which of the following is not a reason to use encapsulation when designing a class?
Q12
How is encapsulation achieved in Java?
Q13
Which access modifier should instance variables normally have?
Q14
A method that only reports the value of a field is called:
Q15
By convention, a method returning a boolean property of an object is named:
Q16
In a UML class diagram, what does the symbol - before an attribute mean?
Q17
In UML, + setName(name : String) : void tells you that:
Q18
A driver class is:
Q19
Which of the following is true of instance variables?
Q20
Complete the following program so that it compiles and prints 1 then 2.
class MyClass {
int count = 0;
void incrementCount() {
________________; // Step 1: increment count
________________; // Step 2: print the updated count
}
public static void main(String[] args) {
________________; // Step 3: create an object
________________; // Step 4: first call
________________; // Step 5: second call
}
}
Which set of lines is correct?
Q21
What is the output?
public class Box {
private int size = 1;
public void grow() { int size = 10; }
public int getSize() { return size; }
public static void main(String[] args) {
Box b = new Box();
b.grow();
System.out.print(b.getSize());
}
}
Q22
Which statement about an instance method is correct?
Q23
What is a helper method?
Q24
Two objects a and b of the same class have different values in their fields.
This is possible because:
Q25
Which line correctly declares an object variable without creating an object?
Q26
Suppose the variable declared in the previous question is an instance variable (a field), so
it holds null by default. What happens if a method is called on it before an object is
assigned to it?
Q27
Which of the following best describes abstraction in OOP?
Q28
What does public String toString() conventionally do?
Q29
What is the output?
public class Temperature {
private double celsius;
public void setCelsius(double c) { if (c >= -273.15) celsius = c; }
public double getFahrenheit() { return celsius * 9 / 5 + 32; }
public static void main(String[] args) {
Temperature t = new Temperature();
t.setCelsius(-500);
System.out.print(t.getFahrenheit());
}
}
Q30
The validation in the previous question is an example of:
Q31
Which sequence of statements is correct in a driver class?
Q32
In the class body, where must instance variables be declared?
Q33
Which of the following would you expose as a public method of a BankAccount
class?
Q34
Given Student a = new Student(); Student b = a;, how many Student objects exist?
Q35Short answer
Explain the difference between a class and an object to someone who
has never programmed, using an example that is not Student or Car.
Q36Short answer
A colleague makes every field public “because getters and
setters are just extra typing”. Give two concrete problems this will cause in a class
representing a bank account.
Q37Short answer
Explain why this is necessary in
public void setId(int id) { this.id = id; } but not in
public void setId(int newId) { id = newId; }.
Q38ProgrammingProgramming (lab-quiz style)
Design a Student class with private attributes
id (int), name (String), age (int) and grade (double); getters and setters for
all four; a method displayInfo() that prints all the details; and a method isPassed()
returning true when the grade is at least 50. Then write a separate driver class
StudentApp whose main creates two students, modifies one of them with setters, and calls
displayInfo() and isPassed() for each, printing Status: Passed or
Status: Failed.