Unit 5: Writing Classes
Showing 17 of 17 questions
What is the output of the following code?
What is the output of the following code?
What is the output of the following code?
What is the output of the following code?
What is the output of the following code?
What is the output of the following code?
What is the output of the following code?
A static method:
In a Java class, the keyword "this" refers to:
Method overloading occurs when:
A variable declared inside a for loop:
When System.out.println(myObject) is called, Java automatically invokes:
A static variable in Java:
Consider the following class. \npublic class Counter { private int count; private int limit; public Counter(int limit) { this.limit = limit; count = 0; } public void increment() { if (count < limit) count++; } public int getCount() { return count; } } \nWhat is the output after the following code executes? \nCounter c = new Counter(3);\nfor (int i = 0; i < 5; i++) { c.increment(); }\nSystem.out.println(c.getCount());
A class Student has private instance variables String name and double gpa. The equals method is written as: \npublic boolean equals(Object obj) { Student other = (Student) obj; return name.equals(other.name) && gpa == other.gpa; } \nWhich of the following could cause this method to throw an exception at runtime?
Consider the following class: public class Counter { private static int count = 0; private int id; public Counter() { count++; id = count; } public static int getCount() { return count; } public int getId() { return id; } } After executing: Counter a = new Counter(); Counter b = new Counter(); Counter c = new Counter(); What are the values of a.getId(), c.getId(), and Counter.getCount()?
What is the output? public class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public void translate(int dx, int dy) { x += dx; y += dy; } public String toString() { return "(" + x + "," + y + ")"; } } Point p = new Point(3, 4); Point q = p; q.translate(1, 1); System.out.println(p);
Advertisement