Which loop is guaranteed to execute at least once in Java?
do-while
What is the purpose of a method in Java?
To encapsulate a set of instructions for reuse.
Which class do you use to read data from a text file?
A. File
B. PrintWriter
C. Scanner
D. System
C. scanner
The keyword __________ is required to define a class.
A. static
B. void
C. class
D. int
C. class
Object-oriented programming allows you to derive new classes from existing classes. This is called ____________.
A. encapsulation
B. inheritance
C. abstraction
D. generalization
B. inheritance
What is the purpose of the continue statement in a loop?
Skips the current iteration and continues with the next iteration of the loop.
What keyword is used to define a method in Java?
no keyword
Which of the following is not an advantage of Java exception handling?
A. Java separates exception handling from normal processing tasks.
B. Exception handling improves performance.
C. Exception handling makes it possible for the caller's caller to handle the exception.
D. Exception handling simplifies programming because the error-reporting and error-handling code can be placed at the catch block.
B. Exception handling improves performance.
An object is an instance of a __________.
A. program
B. class
C. method
D. data
B. class
Polymorphism means ______________.
A. that data fields should be declared private
B. that a class can extend another class
C. that a variable of supertype can refer to a subtype object
D. that a class can contain another class
C. that a variable of supertype can refer to a subtype object
What is the syntax for a for loop in Java?
for (initialization; condition; iteration) {
// code to be executed
}
What is method overloading in Java?
Defining multiple methods with the same name but different parameter lists.
An instance of _________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.
A. RuntimeException
B. Exception
C. Error
D. Throwable
E. NumberFormatException
A. RuntimeException
Analyze the following code.
class TempClass {
int i;
public void TempClass(int j) {
int i = j;
}
}
public class C {
public static void main(String[] args) {
TempClass temp = new TempClass(2);
}
}
A. The program has a compile error because TempClass does not have a default constructor.
B. The program has a compile error because TempClass does not have a constructor with an int argument.
C. The program compiles fine, but it does not run because class C is not public.
D. The program compiles and runs fine.
B. The program has a compile error because TempClass does not have a constructor with an int argument.
public class Test {
public static void main(String[] args) {
new B();
}
}
class A {
int i = 7;
public A() {
System.out.println("i from A is " + i);
}
public void setI(int i) {
this.i = 2 * i;
}
}
class B extends A {
public B() {
setI(20);
// System.out.println("i from B is " + i);
}
@Override
public void setI(int i) {
this.i = 3 * i;
}
}
A. The constructor of class A is not called.
B. The constructor of class A is called and it displays "i from A is 7".
C. The constructor of class A is called and it displays "i from A is 40".
D. The constructor of class A is called and it displays "i from A is 60".
B. The constructor of class A is called and it displays "i from A is 7".
public class InfiniteLoop {
public static void main(String[] args) {
int j = 0;
while (j >= 0) {
System.out.println("A loop that never terminates because its termination condition is never met.
");
}
}
}
Write a method declaration for a method named isOdd that takes an int parameter and returns true if it's odd, false otherwise.
public boolean isOdd(int number)
{
return number % 2 == 1;
}
Which method can be used to create an input object for file temp.txt?
A. new Scanner("temp.txt")
B. new Scanner(temp.txt)
C. new Scanner(new File("temp.txt"))
D. new Scanner(File("temp.txt"))
C. new Scanner(new File("temp.txt"))
public class Foo {
int i;
static int s;
public static void main(String[] args) {
Foo f1 = new Foo();
System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s);
Foo f2 = new Foo();
System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s);
Foo f3 = new Foo();
System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s);
}
public Foo() {
i++;
s++;
}
}
A. f2.i is 1 f2.s is 1
B. f2.i is 1 f2.s is 2
C. f2.i is 2 f2.s is 2
D. f2.i is 2 f2.s is 1
B. f2.i is 1 f2.s is 2
Which of the following declares an abstract method in an abstract Java class?
A. public abstract method();
B. public abstract void method();
C. public void abstract method();
D. public void method() {}
E. public abstract void method() {}
B. public abstract void method();
Write a code snippet that takes an integer input from the user and then prints the digits of the input number in reverse order.
Given 1234:
the output will be 4321.
import java.util.Scanner;
public class reversingInteger {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
while (n>0)
{
int digit = n % 10;
System.out.print(digit);
n=n/10;
}
System.out.println();
}
}
Write a method call for the method isOdd with argument 10.
boolean result = isOdd(10);
I:
File file = new File("input.txt");
try (Scanner input = new Scanner(file);) {
String line = input.nextLine();
}
II:
try (File file = new File("input.txt");
Scanner input = new Scanner(file);) {
String line = input.nextLine();
}
III:
File file;
try (file = new File("input.txt");
Scanner input = new Scanner(file);) {
String line = input.nextLine();
}
IV:
File file;
Scanner input;
try (file = new File("input.txt");
input = new Scanner(file);) {
String line = input.nextLine();
}
A. I
B. II
C. III
D. IV
A. I
public class Test {
public static void main(String args[]) {
NClass nc = new NClass();
nc.t = nc.t++;
}
}
class NClass {
int t;
private NClass() {
}
}
A. The program has a compile error because the NClass class has a private constructor.
B. The program does not compile because the parameter list of the main method is wrong.
C. The program compiles, but has a runtime error because t has no initial value.
D. The program compiles and runs fine.
A. The program has a compile error because the NClass class has a private constructor.
interface A {
}
class C {
}
class D extends C {
}
class B extends D implements A {
}
public class Test {
public static void main(String[] args) {
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}
A. Nothing.
B. b is an instance of A.
C. b is an instance of C.
D. b is an instance of A followed by b is an instance of C.
D. b is an instance of A followed by b is an instance of C.