Textbook Questions Flashcards
(30 cards)
How many times will the following code print ‘Welcome to Java’? int count = 0; while (count < 10) { System.out.println(‘Welcome to Java’); count++; }
10
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(); } } }
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.
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; }
1
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’); } }
i is 4 followed by 9 is not prime
Analyze the following code fragment: int x = 1; while (0 < x) && (x < 100) System.out.println(x++);
The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses.
Which of the following loop prints welcome to java 10 times?
for (int count = 0; count < 10; count++) { System.out.println(‘Welcome to Java’); }
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);
10
The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + ‘ ‘); i++; }
1 3 5 7 9
What is y after the following for loop statement is executed? int y = 0; for (int i = 0; i < 10; ++i) { y += 1; }
10
Which of the loop statements always have their body executed at least once?
The do-while loop
Analyze the following code: class Test { public static void main(String[] args) { String s; System.out.println(‘s is ‘ + s); } }
The program has a compilation error because s is not initialized, but it is referenced in the println statement.
Which of the following statements will convert a string s into a double value d?
All of the listed options.
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); } }
The program has a compilation error because class A does not have a default constructor.
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’); } }
Java and HTML
Which of the following is not an object?
342
Which of the following is the correct statement to return a string from an array a of characters?
new String(a)
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); } }
The program has compile errors because the variable radius is not initialized.
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’); } }
s1 and s2 have different contents
Which of the following statements convert a double value d into a string s?
s = (new Double(d)).toString();
Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class. public MyClass(){ xMethod(); }
a static method or an instance method
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()); } }
Person Person
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(); }
Must implement a method called calculate, and will not be able to access the instance variable a.
Superclass methods with this level of access cannot be called from subclasses.
private
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(); } }
‘The default constructor of A is invoked’‘The default constructor of B is invoked’