Class Anatomy & Instance Variables (Questions 1-12)
Question 1
Consider the following class. What happens when the code in main executes?
public class Dog {
private String name;
private int age;
}
// In main:
Dog d = new Dog();
System.out.println(d.name);
Question 2
What are the default values for these instance variables?
public class Example {
private int number;
private double value;
private boolean flag;
private String text;
}
Question 3
Which access modifier makes an instance variable visible only within its own class?
Question 4
What is printed?
public class Counter {
private int count = 10;
public int getCount() {
return count;
}
}
// In main:
Counter c1 = new Counter();
Counter c2 = new Counter();
System.out.println(c1.getCount() + c2.getCount());
Question 5
What is the primary purpose of encapsulation?
Question 6
What is printed?
public class Box {
private int width;
private int height = 5;
public int getArea() {
return width * height;
}
}
// In main:
Box b = new Box();
System.out.println(b.getArea());
Question 7
What is printed?
public class Point {
private int x;
private int y;
public void setX(int x) {
x = x;
}
public int getX() {
return x;
}
}
// In main:
Point p = new Point();
p.setX(10);
System.out.println(p.getX());
Question 8
What is printed?
public class Point {
private int x;
private int y;
public void setX(int newX) {
this.x = newX;
}
public int getX() {
return this.x;
}
}
// In main:
Point p = new Point();
p.setX(10);
System.out.println(p.getX());
Question 9
Which correctly declares an accessor (getter) method for a private variable called score?
Question 10
Which correctly declares a mutator (setter) method for a private int called age?
Question 11
What is the scope of a local variable declared inside a method?
Question 12
What happens if a local variable has the same name as an instance variable?
public class Test {
private int value = 100;
public void demo() {
int value = 50;
System.out.println(value);
}
}
Constructors (Questions 13-24)
Question 13
What is true about constructors?
Question 14
What is printed?
public class Student {
private String name;
private int grade;
public Student(String n, int g) {
name = n;
grade = g;
}
public int getGrade() {
return grade;
}
}
// In main:
Student s = new Student("Alice", 11);
System.out.println(s.getGrade());
Question 15
What happens when this code executes?
public class Car {
private String model;
public Car(String m) {
model = m;
}
}
// In main:
Car c = new Car();
Question 16
What is printed?
public class Book {
private String title;
private int pages;
public Book() {
title = "Unknown";
pages = 0;
}
public Book(String t, int p) {
title = t;
pages = p;
}
public String getTitle() {
return title;
}
}
// In main:
Book b = new Book();
System.out.println(b.getTitle());
Question 17
What is printed?
public class Circle {
private double radius;
public Circle() {
this(1.0);
}
public Circle(double r) {
radius = r;
}
public double getRadius() {
return radius;
}
}
// In main:
Circle c = new Circle();
System.out.println(c.getRadius());
Question 18
Where must this() or super() appear in a constructor?
Question 19
What is wrong with this constructor?
public class Person {
private String name;
public void Person(String n) {
name = n;
}
}
Question 20
What is printed?
public class Item {
private static int count = 0;
private int id;
public Item() {
count++;
id = count;
}
public int getId() {
return id;
}
}
// In main:
Item a = new Item();
Item b = new Item();
Item c = new Item();
System.out.println(a.getId() + " " + b.getId() + " " + c.getId());
Question 21
What is printed?
public class Rectangle {
private int width;
private int height;
public Rectangle(int w, int h) {
width = w;
height = h;
}
public Rectangle(int side) {
this(side, side);
}
public int getArea() {
return width * height;
}
}
// In main:
Rectangle r = new Rectangle(5);
System.out.println(r.getArea());
Question 22
How many constructors can a class have?
Question 23
What happens if you don't write any constructor for a class?
Question 24
What is printed?
public class Account {
private double balance;
public Account() {
balance = 100;
}
public Account(double initial) {
balance = initial;
}
public double getBalance() {
return balance;
}
}
// In main:
Account a1 = new Account();
Account a2 = new Account(500);
System.out.println(a1.getBalance() + a2.getBalance());
Methods & Return Values (Questions 25-36)
Question 25
What is printed?
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
// In main:
Calculator c = new Calculator();
System.out.println(c.add(3, 4));
Question 26
What is printed?
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
// In main:
Calculator c = new Calculator();
System.out.println(c.add(3.0, 4.0));
Question 27
What is printed?
public class Counter {
private int value = 0;
public int increment() {
value++;
return value;
}
public int getValue() {
return value;
}
}
// In main:
Counter c = new Counter();
System.out.println(c.increment());
System.out.println(c.increment());
System.out.println(c.getValue());
Question 28
What is printed?
public class MathHelper {
public int square(int n) {
return n * n;
}
public int sumOfSquares(int a, int b) {
return square(a) + square(b);
}
}
// In main:
MathHelper m = new MathHelper();
System.out.println(m.sumOfSquares(3, 4));
Question 29
What is printed?
public class Processor {
public void modify(int x) {
x = x * 2;
}
}
// In main:
Processor p = new Processor();
int num = 10;
p.modify(num);
System.out.println(num);
Question 30
What happens when a method has no return statement but declares a return type?
public int getValue() {
int x = 5;
// no return statement
}
Question 31
What is printed?
public class StringUtil {
public String repeat(String s, int times) {
String result = "";
for (int i = 0; i < times; i++) {
result += s;
}
return result;
}
}
// In main:
StringUtil su = new StringUtil();
System.out.println(su.repeat("ab", 3));
Question 32
What is true about method overloading?
Question 33
What is printed?
public class Checker {
public boolean isEven(int n) {
return n % 2 == 0;
}
public boolean isBetween(int n, int low, int high) {
return n >= low && n <= high;
}
}
// In main:
Checker c = new Checker();
System.out.println(c.isEven(7) + " " + c.isBetween(5, 1, 10));
Question 34
What is printed?
public class Grader {
public String getGrade(int score) {
if (score >= 90) return "A";
if (score >= 80) return "B";
if (score >= 70) return "C";
return "F";
}
}
// In main:
Grader g = new Grader();
System.out.println(g.getGrade(85) + g.getGrade(65));
Question 35
What is printed?
public class Mystery {
public int compute(int n) {
if (n <= 1)
return n;
return compute(n - 1) + compute(n - 2);
}
}
// In main:
Mystery m = new Mystery();
System.out.println(m.compute(5));
Question 36
What keyword is used to return early from a void method?
Static vs Instance (Questions 37-44)
Question 37
What is printed?
public class Counter {
private static int count = 0;
public Counter() {
count++;
}
public static int getCount() {
return count;
}
}
// In main:
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
System.out.println(Counter.getCount());
Question 38
What is the main difference between static and instance variables?
Question 39
What happens when you try to access an instance variable from a static method?
public class Example {
private int value = 10;
public static void printValue() {
System.out.println(value);
}
}
Question 40
How do you call a static method?
public class MathUtils {
public static int square(int n) {
return n * n;
}
}
Question 41
What is printed?
public class Test {
private int x = 5;
private static int y = 10;
public void show() {
System.out.print(x + " " + y);
}
}
// In main:
Test t = new Test();
t.show();
Question 42
What is printed?
public class Bank {
private static double interestRate = 0.05;
private double balance;
public Bank(double b) {
balance = b;
}
public static void setRate(double r) {
interestRate = r;
}
public double getInterest() {
return balance * interestRate;
}
}
// In main:
Bank b1 = new Bank(1000);
Bank b2 = new Bank(2000);
Bank.setRate(0.10);
System.out.println(b1.getInterest() + " " + b2.getInterest());
Question 43
Can a static method call an instance method directly?
Question 44
Which method in the Math class is an example of a static method?
Object References (Questions 45-50)
Question 45
What is printed?
public class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
// In main:
Point p1 = new Point(3, 4);
Point p2 = p1;
p2.x = 10;
System.out.println(p1.x);
Question 46
What is printed?
public class Box {
public int value;
public Box(int v) {
value = v;
}
}
// In main:
Box b1 = new Box(5);
Box b2 = new Box(5);
System.out.println(b1 == b2);
Question 47
What is printed?
public class Modifier {
public void change(Point p) {
p.x = 100;
}
}
public class Point {
public int x = 5;
}
// In main:
Point pt = new Point();
Modifier m = new Modifier();
m.change(pt);
System.out.println(pt.x);
Question 48
What is printed?
public class Reassigner {
public void change(Point p) {
p = new Point(100);
}
}
public class Point {
public int x;
public Point(int x) { this.x = x; }
}
// In main:
Point pt = new Point(5);
Reassigner r = new Reassigner();
r.change(pt);
System.out.println(pt.x);
Question 49
What is the value of a null reference?
String s = null;
System.out.println(s);
Question 50
What happens when this code runs?
String s = null;
System.out.println(s.length());
🎉 Unit 2 Complete!
Review your answers above or click Reset to practice again.
Contact form
Choosing a selection results in a full page refresh.