CS 2336 - Computer Science II

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What exception type does the following program throw? public class Test { public static void main (String[] args){ int[] list = new list [5]; System.out.printkn(list[5]); } }

ArrayIndexOutOfBoundsException

A method must declare to throw ___.

Checked exceptions

Suppose list is a LinkedList that contains 1 million int values. Analyze the following code: A: for (int i = 0; i < list.size(); i++) sum += list.get(i); B: for (int i: list) sum += i;

Code fragment B runs faster than code fragment A.

Assume Cylinder is a subtype of Circle. Analyze the following code: Cylinder cy = new Cylinder(1, 1); Circle c = cy;

Code is fine

Fill in the most appropriate code in the blanks in the MyInt class? public class MyInt implements _______ { int id; public MyInt(int id) { this.id = id; } public String toString() { return String.valueOf(id); } public int compareTo(_______ arg0) { if (id > arg0.id) return 1; else if (id < arg0.id) return -1; else return 0; } }

Comparable<MyInt> / MyInt

The ________ method parses a string s to a double value.

Double.parseDouble(s);

An instance of ________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully.

Error

The iterator() method is defined in the ________ interface.

Iterable

The ________ of a path is the number of the edges in the path.

Length

________ measures how full the hash table is.

Load factor

Which of the following data types does not implement the Collection interface?

Map

How do you write 2.5 ^ 3.1 in Java?

Math.pow(2.5, 3.1)

Will System.out.println((char)4) display 4?

No

What exception type does the following program throw?

NullPointerException

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

The loop does not end

Analyze the following code: public class Test { public static void main(String[] args) { B b = new B(); b.m(5); System.out.println("i is " + b.i); } } class A {int i; public void m(int i) { this.i = i; } } class B extends A { public void m(String s) {} }

The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

Analyze the following code: public class Test { public static void main (String[] args){ B b = new B(); b.m(5); System.out.println("i$ is " + b.i); } } class A { int i; public void m(int i) { this.i = I; } } class B extends A { public void m(String s) { } }

The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

Analyze the following recursive method. public static long factorial(int n) { return n * factorial(n - 1); }

The method runs infinitely and causes a StackOverflowError.

Analyze the following code: public class Test { public static void main (String[] args){ System.out.println(max(1, 2)); } public static double max(int num1, double num2) { System.out.println("max(int, double) is invoked"); if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { System.out.println("max(double, int) is invoked"); if (num1 > num2) return num1; else return num2; } }

The program cannot compile because the compiler cannot determine which max method should be invoked

Analyze the following code: public class Test { public static void main (String[] args){ int[] x ={1, 2, 3, 4}; int[] y = x; x = new int[2]; for (int i = 0; i < y.length; i++) System.out.println(y[i] + " "); } }

The program displays 1 2 3 4

Analyze the following code: public class Test { public static void main (String[] args){ int[] oldList = {1, 2, 3, 4, 5}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " "); } public static void reverse(int[] list){ int[] newList = new int [list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.lenght - 1 - i]; list = newList; } }

The program displays 1 2 3 4 5

Analyze the following code: public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main (String[] args){ Test test = new Test(); System.out.println(test.x); } }

The program has a compile error because Test does not have a default constructor

Analyze the following code: public class Test { public static void main (String[] args){ NClass nc = new NClass(); nc.t = nc.t++; } } class NClass { int t; private NClass() { } }

The program has a compile error because the NClass class has a private constructor

Analyze the following code: public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main (String[] args){ Test test = null; System.out.println(test.x); } }

The program has a runtime NullPointerException because test is null while executing test.x.

Analyze the following code: public class Test { public static void main (String[] args){ int[] x = new int[5]; int i; for (i = 0; i < x.length; i++) x[1] = i; System.out.println(x[i]); } }

The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException

Analyze the following code: class Test { public static void main (String[] args){ StringBuilder strBuf = new StringBuilder(4); strBuf.append("ABCDE"); System.out.println("What's strBuf.chatAt(5)? " + strBuf.charAt(5)); } }

The program has a runtime error because the length of the string in the buffer is 5 after "ABCDE" is appended into the buffer. Therefore, strBuf.charAt(5) is out of range.

Analyze the following code: 1. public class Test { 2. public static void main(String[] args) { 3. Fruit[] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)}; 4. java.util.Arrays.sort(fruits); 5. } 6. } class Fruit { private double weight; public Fruit(double weight) { this.weight = weight; } }

The program has a runtime error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.

Analyze the following code: public class Test { public static void main (String[] args){ boolean[][] x = new boolean[3][]; x[0] = new boolean[1]; x[1] = new Boolean[2]; x[2] = new boolean[3]; System.out.println("x[2][2] is " + x[2][2]); } }

The program runs and displays x[2][2] is false

Analyze the following code: public class Test { public static void main (String[] args){ int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } }

The program runs fine and displays x[0] is 0

A Java exception is an instance of ________.

Throwable

Two objects have the same hashCodes if they are equal.

True

What is displayed on the console when running the following program?

Welcome to Java followed by The finally clause is executed in the next line

Are the following four statements equivalent? number += 1; number = number + 1; number++; ++number;

Yes

Does <? super Number> represent a superclass of Number?

Yes

Is ArrayList<?> same as ArrayList<? extends Object>?

Yes

Is ArrayList<Integer> a subclass of ArrayList<? extends Number>?

Yes

Which of the statements regarding the super keyword is incorrect?

You can use super.super.p to invoke a method in superclass's parent class

Analyze the following code: public class Test { public static void main(String[] args) throws MyException { System.out.println("Welcome to Java"); } } class MyException extends Error {}

You should not declare a class that extends Error, because Error raises a fatal error that terminates the program.

Given the following classes and their objects: class c1{}; class C2 extends C1{}; class C3 extends C1{}; C2 c2 = new C2(); C3 c3 = new C3(); Analyze the following statement: c2 = (C2)((C1)c3);

You will get a runtime error because you cannot cast objects from sibling classes

A method that is associated with an individual object is called ______

an instance method

char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character _____.

between 'a' and 'z'

Suppose x is a char variable with a value 'b'. What is the output of the statement System.out.println(++x)?

c

For an instance of Collection, you can obtain its iterator using ____

c.iterator()

1. Given the following code: class C1 {} class C2 extends C1 {} class C3 extends C2 {} class C4 extends C1 {} C1 c1 = new C1(); C2 c2 = new C2(); C3 c3 = new C3(); C4 c4 = new C4();

c4 instanceof C2

A method must declare to throw ________.

checked exceptions

An object is an instance of a ________

class

Analyze the following code: public class Test {public static void main(String[] args) { System.out.println(f1(3)); System.out.println(f2(3, 0)); } public static int f1(int n) { if (n == 0) return 0: else { return n + f1(n - 1): } } public static int f2(int n, int result) { if (n == 0) return result; else return f2(n - 1, n + result); }}

f2 is tail recursion, but f1 is not

Which of the following classes cannot be extended?

final class A { }

What is the output after the following loop terminates?

i is 5 isPrime is false

What is the output for the third statement in the main method? public class Foo { static int i = 0; static int j = 0; public static void main (String[] args){ int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } }

j is 0

Suppose List<String> list = new ArrayList<String>. Which of the following operations is correct?

list.add("Red");

In the following method, what is the base case? static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); }

n is 1

You can create an ArrayList using ____.

new ArrayList<>()

The visibility of these modi!ers increases in this order:_____

private, none (if no modi!er is used), protected, and public

To declare a class named A with two generic types, use ____.

public class A<E, F> { ... }

All Java applications must have a method ___.

public static void main(String[] args)

Analyze the following code: public class Test { public static void main (String[] args){ String s = new String("Welcome to Java"); Object o = s; String d = (string)o; } }

s, o, and d reference the same String object.

Analyze the following code: public class Test { public static void main(String[] args) { String s = new String("Welcome to Java"); Object o = s; String d = (String)o; } }

s, o, and d reference the same String object.

Assume s is " abc ", the method ________ returns a new string "abc".

s.trim()

Analyze the following code: public class Test { private int t; public static void main (String[] args){ int x; System.out.println(t); } }

t is non-static and it cannot be referenced in a static context in the main method

Analyzing algorithm efficiency is ________.

to estimate their growth function

Assume x = 4 and y = 5. Which of the following is true?

x < 5 || y < 5

Given the declaration Circle x = new Circle(), which of the following statements is most accurate?

x contains a reference to a Circle object

Given the declaration Circle[] x = new Circle[10], which of the following statements is most accurate?

x contains a reference to an array and each element in the array can hold a reference to a Circle object

What is the output of the following code? int x = 0; while (x < 4) { x = x + 1; } System.out.println("x is " + x);

x is 4

Which of the following expressions results in a value 1?

37 % 6

What is Math.round(3.6)?

4

Assume double[][] x = new double[4][5], what are x.length and x[2].length?

4 and 5

Assume double[][][] x = new double[4][5][6], what are x.length, x[2].length, and x[0][0].length?

4, 5, and 6

What is Math.rint(3.5)?

4.0

How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < i; j++) System.out.println(i * j)

45

To create a generic type bounded by Number, use ________.

<E extends Number>

The equal comparison operator in Java is ________.

==

Which of the following is incorrect?

A constructor may be static

Analyze the following code: class Test { public static void main(String[] args) { try { int zero = 0; int y = 2/zero; try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException } catch(Exception e) { } } catch(RuntimeException e) { System.out.println(e); } } }

A good programming practice is to avoid nesting try-catch blocks, because nesting makes programs difficult to read. You can rewrite the program using only one try-catch block.

Analyze the following code: public class Test { public static void main (String[] args){ try { int zero = 0; int y = 2/zerp; try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException } catch(Exception e) { } catch (RuntimeException e) { System.out.println(e); } } }

A good programming practice is to avoid nesting try-catch blocks, because nesting makes programs difficult to read. You can rewrite the program using only one try-catch block.

A variable defined inside a method is referred to as

A local variable

Analyze the following program. public class Test { public static void main(String[] args) { try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatExceptionint i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (Exception ex) { System.out.println(ex); } } }

An exception is raised due to Integer.parseInt(s);

Which of the following is the correct statement to return JAVA?

"Java".toUpperCase()

To check whether a char variable ch is an uppercase letter, you write ________.

(ch >= 'A' && ch <= 'Z')

The statement System.out.printf("%10s", 123456) outputs ________. (Note: * represents a space.)

****123456

Given the following method static void nPrint(String message, int n){ while (n > 0) { System.out.println(message); n--; } } What is k after invoking nPrint("A message",k)? int k = 2; nPrint("A message", k);

2

If you enter 1 2 3, when you run this program, what will be the output? import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three numbers: "); double number1 = input.nextDouble(); double number2 = input.nextDouble(); double number3 = input.nextDouble(); // Compute average double average = (number1 + number2 + number3) / 3; // Display result System.out.println(average); } }

2.0

What is the value of (double)(5/2)?

2.0

What is the value of (double)5/2?

2.5

The expression 4 + 20 / (3 - 1) ∗ 2 is evaluated to ________.

24

"AbA".compareToIgnoreCase("abC") returns ________.

-2

-25 % 5 is ________.

0

The ________ method in the Queue interface retrieves and removes the head of this queue, or null if this queue is empty.

1. poll()

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

10

For a sorted list of 1024 elements, a binary search takes at most ________ comparisons.

11

What is the result of 45 / 4?

11

If each key is mapped to a different index in the hash table, it is called ________.

Perfect hashing

The ________ is to visit the current node first, then the left subtree of the current node, and finally the right subtree of the current node.

Preorder traversal

Analyze the following code: A: public class Test { public static void main(String[] args) { xMethod(5); } public static void xMethod(int length) { if (length > 1) { System.out.print((length - 1) + " "); xMethod(length - 1); } } } B: public class Test { public static void main(String[] args) { xMethod(5); } public static void xMethod(int length) { while (length > 1) { System.out.print((length - 1) + " "); xMethod(length - 1); } } }

Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 .... 1 infinitely.

An instance of ________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.

RuntimeException

Analyze the following code and choose the best answer: public class Foo { private int x; public static void main (String[] args){ Foo foo = new foo(); System.out.println(foo.x); } }

Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code.

Analyze the following code: int[] list = new int[5]; list = new int[6];

The code can compile and run fine. The second line assigns a new array to list.

Analyze the following code: 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); } public void setI(int i) { this.i = 3 * i; } }

The constructor of class A is called and it displays "i from A is 7".


Ensembles d'études connexes

70-410 1.TS Installing and Configure Servers

View Set

The Study of Contemporary Issues

View Set

Economics Chapter 11, Economics Chapter 11, Economics Chapter 12, Economics Chapter 12, Econ. Chapter 11, Econ. Chap 12, Economics Chapter 13, Econ. Chap. 14, Economics Chapter 14, Economics Chapter 13, Economics Chapter 14

View Set