CS111 FInal

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

When you pass an array to a method, the method receives ________. Select one: a. a copy of the first element b. the length of the array c. a copy of the array d. a reference to the array

a reference to the array

Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as ________, which stores elements in last-in first-out fashion. Select one: a. storage area b. an array c. a heap d. a stack

a stack

To place a new element e at the back of an initialized Queue queue, one uses the statement "queue. Answer(e);". To return (but not remove) the front element of an initialized Queue queue, one uses the expression "queue.Answer ()". To remove and return the front element of an initialized Queue queue, one uses the expression "queue.Answer ()".

offer remove peek

What is the output of 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 b. s1 and s2 have the same contents c. s1 and s2 reference to the same String object

s1 and s2 have different contents

Match the set operations to the methods of the Set interface used to perform them. set union Answer 1Choose...addAllremoveAllcontainsretainAllcontainsAll set difference Answer 2Choose...addAllremoveAllcontainsretainAllcontainsAll set intersection Answer 3Choose...addAllremoveAllcontainsretainAllcontainsAll set membership Answer 4Choose...addAllremoveAllcontainsretainAllcontainsAll superset

set union → addAll, set difference → removeAll, set intersection → retainAll, set membership → contains, superset → containsAll

Which of the following lines is not a Java comment? a. -- comments b. /* comments */ c. /** comments */ d. // comments e. ** comments **

-- comments, ** comments **

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); } } Select one: a. j is 0 b. j is 1 c. j is 2 d. j is 3

0

What is the output of the following program?import java.util.Date;public class Test {public static void main(String[] args) {Date date = new Date(1234567);m1(date); System.out.print(date.getTime() + " ");m2(date); System.out.println(date.getTime());} public static void m1(Date date) {date = new Date(7654321);} public static void m2(Date date) {date.setTime(7654321);}} Select one: a. 7654321 7654321 b. 7654321 1234567 c. 1234567 1234567 d. 1234567 7654321

1234567 7654321

public class Test { public static void main(String[] args) {int[] x = {1, 2, 3, 4, 5}; increase(x);int[] y = {1, 2, 3, 4, 5}; increase(y[0]); System.out.println(x[0] + " " + y[0]); } public static void increase(int[] x) {for (int i = 0; i < x.length; i++) x[i]++;} public static void increase(int y) { y++; }} Select one: a. 2 1 b. 2 2 c. 0 0 d. 1 2 e. 1 1

2 1

What is the output of the following code?public class Test {public static void main(String[] args) {java.math.BigInteger x = new java.math.BigInteger("3");java.math.BigInteger y = new java.math.BigInteger("7");x.add(y);System.out.println(x);}} Select one: a. 10 b. 11 c. 4 d. 3

3

public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; int v = values[0][0]; for (int row = 0; row < values.length; row++) for (int column = 0; column < values[row].length; column++) if (v < values[row][column]) v = values[row][column]; System.out.print(v); }}

33

What is the output of the following code? public class Test5 { public static void main(String[] args) { int[][] matrix = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; for (int i = 0; i < 4; i++) System.out.print(matrix[1][i] + " "); }} Select one: a. 3 6 10 14 b. 1 2 3 4 c. 2 5 9 13 d. 1 3 8 12 e. 4 5 6 7

4 5 6 7

public class Test { public static void main(String[] args) { int[][][] data = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; System.out.print(data[1][0][0]); }} Select one: a. 1 b. 2 c. 4 d. 5 e. 6

5

________ translates a high-level language program into machine language program.

A compiler

Which of the following statements will convert a string s into a double value d? Select one: a. d = (new Double(s)).doubleValue(); b. d = Double.valueOf(s).doubleValue(); c. d = Double.parseDouble(s); d. All of the above

All of the above

To create an instance of BigDecimal for 454.45, use ________. Select one: a. BigInteger("454.45"); b. new BigDecimal("454.45"); c. new BigInteger(454.45); d. BigInteger(454.45);

BigInteger("454.45");

________ consists of a set of separate programs for developing and testing Java programs, each of which is invoked from a command line. Select one:

Java JDK

If a program compiles fine, but it produces incorrect result, then the program suffers ________. a. a runtime error b. a compilation error c. a logic error

a logical error

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]); }} a. The program has a compile error because new boolean[3][] is wrong. b. The program runs and displays x[2][2] is null. c. The program has a runtime error because x[2][2] is null. d. The program runs and displays x[2][2] is true. Incorrect e. The program runs and displays x[2][2] is false.

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

BigInteger and BigDecimal are immutable Select one: a. true b. false

True

Which of the following statements are true? Select one or more: a. The default constructor is a no-arg constructor. b. Every class has a default constructor. c. A default constructor is provided automatically if no constructors are explicitly declared in the class. d. At least one constructor must always be defined explicitly.

a c

Check all that are true about Monte Carlo Simulation: Select one or more: a. Monte Carlo simulation provides a simple means of probabilistic insight to stochastic systems. b. Monte Carlo simulation works well with low probability events of interest. c. Exact probability computation is necessarily more computationally complex than Monte Carlo simulation. d. Monte Carlo simulation is preferred as it trades off computational memory efficiency for faster computational time.

a. Monte Carlo simulation provides a simple means of probabilistic insight to stochastic systems.

To add BigInteger b1 to b2, you write ________. Select one or more: a. b2 = b1.add(b2); b. b1 = b2.add(b1); c. b2.add(b1); d. b2 = b2.add(b1); e. b1.add(b2);

a. b2 = b1.add(b2); d. b2 = b2.add(b1);

Variables defined inside a method are called ________. Select one: a. local variables b. global variables c. arguments d. parameters

a. local variables

In JDK 1.5, you may directly assign a primitive data type value to a wrapper object. This is called ________. a. auto boxing b. auto casting c. auto unboxing d. auto conversion

auto boxing

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 compiles and runs fine and prints nothing. b. The program would compile and run if you change A a = new A() to A a = new A("5"). c. The program has a compilation error because class A is not a public class. d. The program has a compilation error because class A does not have a default constructor.

b d

Which of the following statements will convert a string s into i of int type? Select one or more: a. i = Integer.valueOf(s); b. i = (new Integer(s)).intValue(); c. i = Integer.valueOf(s).intValue(); d. i = Integer.parseInt(s); e. i = (int)(Double.parseDouble(s));

b. i = (new Integer(s)).intValue(); c. i = Integer.valueOf(s).intValue(); d. i = Integer.parseInt(s); e. i = (int)(Double.parseDouble(s));

What is the output of the following code?public class Test { public static void main(String[] args) {String s1 = new String("Welcome to Java");String s2 = s1;s1 += "and Welcome to HTML";if (s1 == s2)System.out.println("s1 and s2 reference to the same String object");elseSystem.out.println("s1 and s2 reference to different String objects");}} Select one: a. s1 and s2 reference to the same String object b. s1 and s2 reference to different String objects

b. s1 and s2 reference to different String objects

The ________ method copies the sourceArray to the targetArray. Select one: a. System.copyArrays(sourceArray, 0, targetArray, 0, sourceArray.length); b. System.arrayCopy(sourceArray, 0, targetArray, 0, sourceArray.length); c. System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length); Correct d. System.copyarrays(sourceArray, 0, targetArray, 0, sourceArray.length);

c. System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

Below is an implementation of a Monte Carlo Simulation algorithm that estimates the probability of generating n random integers from 0 through n-1 such that each successive integer is not less than the previous integer.For example, for n=2, the probability of generating a monotonically ascending (i.e. non-decreasing) sequence of integers 0 through 1 is exactly 3/4, as {0, 0}, {0, 1}, and {1, 1} are the three non-decreasing sequences of the four possible sequences of two 0's and 1's.Select the true statement below about the behavior of this algorithm. a. There is a compilation error. b. There is a runtime error that causes the algorithm to terminate. c. There is an integer overflow that causes incorrect results. d. The algorithm produces inaccurate results for all n >= 1. e. The algorithm produces accurate estimates for small n only. f. The algorithm produces accurate estimates for all n >= 1.

c. There is an integer overflow that causes incorrect results.

How can you get the word "abc" in the main method from the following call?java Test "+" 3 "abc" 2 a. args[0] b. args[1] c. args[2] d. args[3]

c. args[2]

To construct a new Stack object to hold int values, one would use the Java expression _________. a. new Stack() b. new Stack[int]() c. new Stack<Integer>() d. new Stack(Integer) e. new Stack(Integer i) f. new Stack<int>()

c. new Stack<Integer>()

Which of the following statements convert a double value d into a string s? Select one: a. s = (Double.valueOf(s)).toString(); b. s = String.stringOf(d); c. s = (new Double(d)).toString(); d. s = new Double(d).stringOf();

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

Which of the following statements are correct? Select one or more: a. char[2][2][] charArray = {'a', 'b'}; b. char[][][] charArray = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}}; c. char[][][] charArray = {{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}; d. char[][][] charArray = new char[2][2][];

char[][][] charArray = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}}; char[][][] charArray = new char[2][2][];

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.print(y[i] + " ");}} Select one: a. The program displays 0 0 3 4 b. The program displays 0 0 c. The program displays 0 0 0 0 d. The program displays 1 2 3 4

d. The program displays 1 2 3 4

Which of the following statements is correct? a. Every line in a program must end with a semicolon. b. Every class must end with a semicolon. c. Every comment line must end with a semicolon. d. Every method must end with a semicolon. e. Every statement in a program must end with a semicolon.

e. Every statement in a program must end with a semicolon

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 2 f2.s is 1 b. f2.i is 2 f2.s is 2 c. f2.i is 1 f2.s is 2 d. f2.i is 1 f2.s is 1

f2.i is 1 f2.s is 2

You can define two methods in the same class with the same name and parameter list. Select one: a. true b. false

false

To create a new first-in, first-out (FIFO) Queue of integers called intQueue in its most simplest form, one would use the statement "Queue<Integer> intQueue = new Answer<Integer>();"

linkedlist

Which JDK command is correct to run a Java application in ByteCode.class? a. javac ByteCode.java b. JAVAC ByteCode c. java ByteCode d. javac ByteCode e. java ByteCode.class

java ByteCode

Which one of the following commands is the correct way to redirect file "input.txt" to standard input and redirect standard output to file "output.txt" when executing class Test? a. java Test < input.txt > output.txt b. java Test > input.txt < output.txt c. java Test.class < input.txt > output.txt d. java Test.class > input.txt < output.txt

java Test < input.txt > output.txt

To create a new priority queue of integers called intQueue that would treat the minimum integer has having top priority, one would use the statement "Queue<Integer> intQueue = new Answer<Integer>();"

priorityQueue

The main method header is written as: A. public static void main(string[] args) B. public static void Main(String[] args) C. public static void main(String[] args) D. public static main(String[] args) E. public void main(String[] args)

public static void main(String[] args)

To place a new element e on top of an initialized Stack stack, one uses the statement "stack.Answer (e);". To return (but not remove) the top element of an initialized Stack stack, one uses the expression "stack.Answer ()". To remove and return the top element of an initialized Stack stack, one uses the expression "stack.Answer ()".

push peek pop

To create an association between key k and value v in map m, one would use the statement "m.Answer (k, v);" To see whether or not map m contains key k, one would use the expression "m.Answer (k)". To retrieve the value v associated with key k in map m, one would use the expression "m.Answer (k)". To get the set of keys in a map m to iterate through (as with a for-each loop), one would use the expression "m.Answer ()".

put containsKey get keySet

public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); }} Select one: a. t is non-static and it cannot be referenced in a static context in the main method. b. The variable x is not initialized and therefore causes errors. c. The program compiles and runs fine. d. The variable t is not initialized and therefore causes errors. e. The variable t is private and therefore cannot be accessed in the main method.

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


Set pelajaran terkait

IS 300 Technology Guide 2: Software

View Set

Patho/Pharm Exam 1 Study Guide-- Patho

View Set

Chapter 38: Agents to Control Blood Glucose Levels

View Set

Algebra 2 Test Review: Trigonometric Functions

View Set