Textbook Questions Flashcards

(30 cards)

1
Q

How many times will the following code print ‘Welcome to Java’? int count = 0; while (count < 10) { System.out.println(‘Welcome to Java’); count++; }

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Analyze the following code: import java.util.Scanner; public class Test { public static void main(String[] args) { int sum = 0; for (int i = 0; i < 100000; i++) { Scanner input = new Scanner(System.in); sum += input.nextInt(); } } }

A

The program compiles and runs, but it is not efficient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the value of balance after the following code is executed? int balance = 10; while (balance >= 1) { if (balance < 9) break; balance = balance - 9; }

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Suppose the input for number is 9. What is the output from running the following program? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print(‘Enter an integer: ‘); int number = input.nextInt(); int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } System.out.println(‘i is ‘ + i); if (isPrime) System.out.println(number + ‘ is prime’); else System.out.println(number + ‘ is not prime’); } }

A

i is 4 followed by 9 is not prime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Analyze the following code fragment: int x = 1; while (0 < x) && (x < 100) System.out.println(x++);

A

The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which of the following loop prints welcome to java 10 times?

A

for (int count = 0; count < 10; count++) { System.out.println(‘Welcome to Java’); }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How many times will the following code print ‘Welcome to Java’? int count = 0; do { System.out.println(‘Welcome to Java’); count++; } while (count < 10);

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + ‘ ‘); i++; }

A

1 3 5 7 9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is y after the following for loop statement is executed? int y = 0; for (int i = 0; i < 10; ++i) { y += 1; }

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which of the loop statements always have their body executed at least once?

A

The do-while loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Analyze the following code: class Test { public static void main(String[] args) { String s; System.out.println(‘s is ‘ + s); } }

A

The program has a compilation error because s is not initialized, but it is referenced in the println statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which of the following statements will convert a string s into a double value d?

A

All of the listed options.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Analyze the following code: public class Test { public static void main(String[] args) { A a = new A(); a.print(); } } class A { String s; A(String s) { this.s = s; } void print() { System.out.println(s); } }

A

The program has a compilation error because class A does not have a default constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The following program displays __________. public class Test { public static void main(String[] args) { String s = ‘Java’; StringBuilder buffer = new StringBuilder(s); change(buffer); System.out.println(buffer); } private static void change(StringBuilder buffer) { buffer.append(‘ and HTML’); } }

A

Java and HTML

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which of the following is not an object?

A

342

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following is the correct statement to return a string from an array a of characters?

A

new String(a)

17
Q

Analyze the following code: public class Test { public static void main(String[] args) { double radius; final double PI= 3.15169; double area = radius * radius * PI; System.out.println(‘Area is ‘ + area); } }

A

The program has compile errors because the variable radius is not initialized.

18
Q

What is displayed by the following code? public class Test { public static void main(String[] args) { String s1 = new String(‘Welcome to Java!’); String s2 = s1.toUpperCase(); if (s1 == s2) System.out.println(‘s1 and s2 reference to the same String object’); else if (s1.equals(s2)) System.out.println(‘s1 and s2 have the same contents’); else System.out.println(‘s1 and s2 have different contents’); } }

A

s1 and s2 have different contents

19
Q

Which of the following statements convert a double value d into a string s?

A

s = (new Double(d)).toString();

20
Q

Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class. public MyClass(){ xMethod(); }

A

a static method or an instance method

21
Q

What is the output of the following code? public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); } } class Student extends Person { private String getInfo() { return ‘Student’; } } class Person { private String getInfo() { return ‘Person’; } public void printPerson() { System.out.println(getInfo()); } }

A

Person Person

22
Q

Consider the abstract superclass below. Any concrete subclass that extends class Foo public abstract class Foo { private int a; public int b; public Foo(int aVal, int bVal) { a = aVal; b = bVal; } public abstract int calculate(); }

A

Must implement a method called calculate, and will not be able to access the instance variable a.

23
Q

Superclass methods with this level of access cannot be called from subclasses.

24
Q

What is the output of running class C? class A { public A() { System.out.println(‘The default constructor of A is invoked’); } } class B extends A { public B() { System.out.println(‘The default constructor of B is invoked’); } } public class C { public static void main(String[] args) { B b = new B(); } }

A

‘The default constructor of A is invoked’‘The default constructor of B is invoked’

25
An advantage of inheritance is that:
Objects of a subclass can be treated like objects of their superclass.
26
Consider the classes below, declared in the same file. Which of the following statements is false? class A { int a; public A() { a = 7; } } class B extends A { int b; public B() { b = 8; } }
A reference of type A can be treated as a reference of type B.
27
Private fields of a superclass can be accessed in a subclass
by calling public or protected methods declared in the superclass.
28
Which of the following could be used to declare abstract method method1 in abstract class Class1 (method1 returns an int and takes no arguments)?
public abstract int method1();
29
Which statement best describes the relationship between superclass and subclass types?
A subclass reference can be assigned to a superclass variable, but a superclass reference cannot be assigned to a subclass variable.
30
For which of the following would polymorphism not provide a clean solution?
A program to compute a 5% savings account interest for a variety of clients.