Quiz
CS 110 Multiple Choice Chapters 1-8
Create a free account to finish this quiz
Save your progress and see your final score.
Already have an account? Sign in
Questions in this quiz (67)
1The expression (int)(76.0252175 * 100) / 100 evaluates to _________.
- A76
- B76.0252175
- C76.03
- D76.02
2What is y after the following switch statement?
int x = 0;
int y = 0;
switch (x + 1) {
case 0: y = 0;
case 1: y = 1;
default: y = -1
}- A2
- B1
- C0
- D-1
3Assume x is 0. What is the output of the following statement?
if (x > 0)
System.out.print("x is greater than 0");
else if (x < 0)
System.out.print("x is less than 0");
else
System.out.print("x equals 0");- Ax is less than 0
- Bx is greater than 0
- Cx equals 0
- DNone
4Analyze the following code:
Code 1:
boolean even;
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
boolean even = (number % 2 == 0);- ACode 2 has syntax errors.
- BCode 1 has syntax errors.
- CBoth Code 1 and Code 2 have syntax errors.
- DBoth Code 1 and Code 2 are correct, but Code 2 is better.
5What is the output of the following switch statement?
char ch = 'a';
switch (ch) {
case 'a':
case 'A':
System.out.print(ch); break;
case 'b':
case 'B':
System.out.print(ch); break;
case 'c':
case 'C':
System.out.print(ch); break;
case 'd':
case 'D':
System.out.print(ch);- Aab
- Ba
- Caa
- Dabc
- Eabcd
6What is x after evaluating
x = (2 > 3) ? 2 : 3;- A5
- B2
- C3
- D4
7Analyze the following code.
int x = 0;
if (x > 0);
{
System.out.println("x");
}- AThe value of variable x is always printed.
- BThe symbol x is always printed twice.
- CThe symbol x is always printed.
- DNothing is printed because x > 0 is false.
8To declare a constant MAX_LENGTH inside a method with value 99.98, you write
- Afinal double MAX_LENGTH = 99.98;
- Bdouble MAX_LENGTH = 99.98;
- Cfinal MAX_LENGTH = 99.98;
- Dfinal float MAX_LENGTH = 99.98;
9Which of the following is a constant, according to Java naming conventions?
- Aread
- BMAX_VALUE
- CReadInt
- DTest
10What is y after the following switch statement is executed?
x = 3;
switch (x + 3) {
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}- A1
- B4
- C3
- D2
- E0
11Which of the following code displays the area of a circle if the radius is positive.
- Aif (radius <= 0) System.out.println(radius * radius * 3.14159);
- Bif (radius != 0) System.out.println(radius * radius * 3.14159);
- Cif (radius >= 0) System.out.println(radius * radius * 3.14159);
- Dif (radius > 0) System.out.println(radius * radius * 3.14159);
12What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += I;
}
System.out.println(y);- A45
- B11
- C13
- D12
- E10
13Analyze the following code.
int x = 1;
while (0 < x) && (x < 100)
System.out.println(x++);- AThe loop runs forever.
- BThe code does not compile because the loop body is not in the braces.
- CThe code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses.
- DThe numbers 1 to 99 are displayed.
- EThe numbers 2 to 100 are displayed.
14How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
count++;
} while (count < 10);- A8
- B0
- C11
- D10
- E9
15What is i after the following for loop is finished?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}- A10
- Bundefined
- C9
- D11
16Analyze the following code:
public class Test {
public static void main (String args[]) {
int i = 0;
for (i = 0; i < 10; i++);
System.out.println(i + 4);
}
}- AThe program compiles despite the semicolon (;) on the for loop line, and displays 4.
- BThe program compiles despite the semicolon (;) on the for loop line, and displays 14.
- CThe program has a compile error because of the semicolon (;) on the for loop line.
- DThe program has a runtime error because of the semicolon (;) on the for loop line.
17Which of the following expression yields an integer between 0 and 100, inclusive?
- A(int)(Math.random() * 100 + 1)
- B(int)(Math.random() * 101)
- C(int)(Math.random() * 100)
- D(int)(Math.random() * 100) + 1
18Analyze the following code.
int count = 0;
while (count < 100) {
// Point A
System.out.println("Welcome to Java!");
count++;
// Point B
}
// Point C- Acount < 100 is always false at Point A
- Bcount < 100 is always true at Point B
- Ccount < 100 is always false at Point C
- Dcount < 100 is always true at Point C
- Ecount < 100 is always false at Point B
19What is the output of the following fragment?
int i = 1;
int j = 1;
while (i < 5) {
i++;
j = j * 2;
}
System.out.println(j);- A4
- B8
- C16
- D32
20What is Math.rint(3.5)?
- A3
- B5.0
- C4
- D3.0
- E4.0
21Suppose
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n--;
}
}
What is the printout of the call nPrint('a', 4)?- Aaaaaa
- Baaaa
- Cinvalid call, because 'a' is a character, not a string.
- Daaa
22(char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character __________.
- Abetween 'a' and 'y'
- Bbetween 'b' and 'z'
- Cbetween 'b' and 'y'
- Dbetween 'a' and 'z'
23The signature of a method consists of ____________.
- Amethod name and parameter list
- Breturn type, method name, and parameter list
- Cparameter list
- Dmethod name
24Analyze the following code:
class Test {
public static void main(String[] args) {
System.out.println(xMethod((double)5));
}
public static int xMethod(int n) {
System.out.println("int");
return n;
}
public static long xMethod(long n) {
System.out.println("long");
return n;
}
}- AThe program displays int followed by 5.
- BThe program displays long followed by 5.
- CThe program runs fine but displays things other than 5.
- DThe program does not compile.
25A variable defined inside a method is referred to as __________.
- Aa local variable
- Ba block variable
- Ca global variable
- Da method variable
26When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.
- Apass by name
- Bpass by value
- Cpass by reference
- Dmethod invocation
27Which of the following should be declared as a void method?
- AWrite a method that returns a random integer from 1 to 100.
- BWrite a method that prints integers from 1 to 100.
- CWrite a method that checks whether current second is an integer from 1 to 60.
- DWrite a method that converts an uppercase letter to lowercase.
28Each 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.
- Astorage area
- Ba heap
- Ca stack
- Dan array
29What will be displayed by the following code?
class Test {
public static void main(String[] args) {
int[] list1 = {3, 2, 1};
int[] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
for (int i = list2.length - 1; i >= 0; i--)
System.out.print(list2[i] + " ");
}
}- A2 1 0
- B1 2 3
- C0 1 3
- D3 2 1
- E0 1 2
30What will be displayed by the following code?
class Test {
public static void main(String[] args) {
int[] list1 = {3, 2, 1};
int[] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
for (int i = list2.length - 1; i >= 0; i--)
System.out.print(list2[i] + " ");
}
}- A1 2
- B2 2
- C0 0
- D2 1
- E1 1
31Analyze 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[i] = i;
System.out.println(x[i]);
}
}- AThe program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.
- BThe program displays 4.
- CThe program has a compile error because i is not defined in the last statement in the main method.
- DThe program displays 0 1 2 3 4.
32Assume int[] t = {1, 2, 3, 4}. What is t.length?
- A0
- B4
- C5
- D3
33What is output of the following code:
public class Test {
public static void main(String[] args) {
int list[] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < list.length; i++)
list[i] = list[i - 1];
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}- A1 2 3 4 5 6
- B1 1 1 1 1 1
- C2 3 4 5 6 6
- D2 3 4 5 6 1
34Suppose int[] numbers = new int[10], what is numbers[0]?
- A0
- Bnull
- Cundefined.
- D10.
35What is the output of the following code?
int[] myList = {1, 2, 3, 4, 5, 6};
for (int i = myList.length - 2; i >= 0; i--) {
myList[i + 1] = myList[i];
}
for (int e: myList)
System.out.print(e + " ");- A6 2 3 4 5 1
- B6 1 2 3 4 5
- C1 1 2 3 4 5
- D1 2 3 4 5 6
- E2 3 4 5 6 1
36Analyze 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 < x.length; i++)
System.out.print(x[i] + " ");
}
}- AThe program displays 0 0
- BThe program displays 0 0 3 4
- CThe program displays 1 2 3 4
- DThe program displays 0 0 0 0
37How many elements are array matrix (int[][] matrix = new int[5][5])?
- A25
- B20
- C30
- D14
38Assume int[][] x = {{1, 2}, {3, 4, 5}, {5, 6, 5, 9}}, what are x[0].length, x[1].length, and x[2].length?
- A2, 3, and 4
- B3, 3, and 3
- C2, 2, and 2
- D3, 3, and 4
- E2, 3, and 3
39Assume double[][][] x = new double[4][5][6], what are x.length, x[2].length, and x[0][0].length?
- A4, 5, and 6
- B5, 5, and 5
- C6, 5, and 4
- D4, 5, and 4
40What will be displayed by the following code?
public class Test {
public static void main(String[] args) {
double[][] m = {{1, 2, 3}, {1.5, 2.5, 3.5}, {0.1, 0.1, 0.1}};
System.out.println(sum(m));
}
public static double sum(double[][] m) {
double sum = 0;
for (int i = 0; i < m.length; i++)
sum += m[i][i];
return sum;
}
}- A4
- B3.0
- C3
- D4.0
- E3.6
41Assume double[][] x = new double[4][5], what are x.length and x[2].length?
- A4 and 5
- B5 and 5
- C5 and 4
- D4 and 4
42Which of the following statements are correct?
- Achar[2][] charArray = {{'a', 'b'}, {'c', 'd'}};
- Bchar[][] charArray = {{'a', 'b'}, {'c', 'd'}};
- Cchar[2][2] charArray = {{'a', 'b'}, {'c', 'd'}};
- Dchar[][] charArray = {'a', 'b'};
43Assume boolean[][] x = new boolean[5][7], what are x.length and x[2].length?
- A5 and 6
- B4 and 5
- C4 and 4
- D5 and 5
- E5 and 7
44Suppose a method p has the following heading:
public static int[][] p()
What return statement may be used in p()?- Areturn 1;
- Breturn new int[][]{{1, 2, 3}, {2, 4, 5}};
- Creturn new int[]{1, 2, 3};
- Dreturn {1, 2, 3};
- Ereturn int[]{1, 2, 3};
45Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are x.length are x[0].length?
- A2 and 3
- B2 and 2
- C3 and 3
- D2 and 1
- E3 and 2
4624 % 5 is _____
- A2
- B0
- C4
- D1
- E3
47If you attempt to add an int, a byte, a long, and a double, the result will be a __________ value.
- Aint
- Blong
- Cbyte
- Ddouble
48What is the result of 45 / 4?
- A11
- B12
- C10
- D11.25
49In Java, the word true is ________.
- Aa Boolean literal
- Ba Java keyword
- Csame as value 0
- Dsame as value 1
50The following code displays ___________.
double temperature = 50
; if (temperature >= 100)
System.out.println("too hot")
; else if (temperature <= 40)
System.out.println("too cold");
else
System.out.println("just right");- Atoo hot
- Btoo cold
- Ctoo hot too cold just right
- Djust right
51The "less than or equal to" comparison operator in Java is __________.
- A<<
- B=<
- C!=
- D<=
- E<
52Analyze the following code fragments that assign a boolean value to the variable even.
Code 1:
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
even = (number % 2 == 0) ? true: false;
Code 3:
even = number % 2 == 0;- ACode 3 has a syntax error, because you attempt to assign number to even.
- BCode 2 has a syntax error, because you cannot have true and false literals in the conditional expression.
- CAll three are correct, but Code 1 is preferred.
- DAll three are correct, but Code 2 is preferred.
- EAll three are correct, but Code 3 is preferred.
53How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 10);- A10
- B8
- C0
- D9
- E11
54What is the value in count after the following loop is executed?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 9);
System.out.println(count);- A11
- B0
- C9
- D8
- E10
55What is the number of iterations in the following loop:
for (int i = 1; i <= n; i++) {
// iteration
}- A2*n
- Bn
- Cn-1
- Dn+1
56What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += I;
}
System.out.println(y);- A45
- B12
- C13
- D10
- E11
57What is i after the following for loop?
int y = 0;
for (int i = 0; i<10; ++i) {
y += I;
}- A9
- B10
- Cundefined
- D11
58__________ is to implement one method in the structure chart at a time from the top to the bottom.
- AStepwise refinement
- BTop-down approach
- CBottom-up and top-down approach
- DBottom-up approach
59Analyze 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;
}
}- AThe program runs and prints 2 followed by "max(double, int)" is invoked.
- BThe program cannot compile because the compiler cannot determine which max method should be invoked.
- CThe program runs and prints "max(int, double) is invoked" followed by 2.
- DThe program runs and prints 2 followed by "max(int, double)" is invoked.
- EThe program cannot compile because you cannot have the print statement in a non-void method.
60Analyze the following code:
public class Test {
public static void main(String[] args) {
System.out.println(xMethod(5, 500L));
}
public static int xMethod(int n, long l) {
System.out.println("int, long");
return n;
}
public static long xMethod(long n, long l) {
System.out.println("long, long");
return n;
}
}- AThe program does not compile because the compiler cannot distinguish which xmethod to invoke.
- BThe program runs fine but displays things other than 5.
- CThe program displays long, long followed by 5.
- DThe program displays int, long followed by 5.
61What is Math.floor(3.6)?
- A5.0
- B3
- C3.0
- D4
62How many elements are in array double[] list = new double[5]?
- A5
- B4
- C6
- D0
63The reverse method is defined in the textbook. What is list1 after executing the following statements?
int[] list1 = {1, 2, 3, 4, 5, 6};
list1 = reverse(list1);- Alist1 is 6 5 4 3 2 1
- Blist1 is 1 2 3 4 5 6
- Clist1 is 0 0 0 0 0 0
- Dlist1 is 6 6 6 6 6 6
64If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.
- A3.4
- B2.0
- C3.5
- D5.5
65In the following code, what is the output for list2?
class Test {
public static void main(String[] args) {
int[] list1 = {1, 2, 3};
int[] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
for (int i = 0; i < list2.length; i++)
System.out.print(list2[i] + " ");
}
}- A1 2 3
- B1 1 1
- C0 1 2
- D0 1 3
66What is the output of the following code?
double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < myList.length; i++) {
if (myList[i] >= max) {
max = myList[i];
indexOfMax = i;
}
}
System.out.println(indexOfMax);- A0
- B1
- C2
- D3
- E4
67Suppose a method p has the following heading:
public static int[][] p()
What return statement may be used in p()?- Areturn 1;
- Breturn {1, 2, 3};
- Creturn int[]{1, 2, 3};
- Dreturn new int[]{1, 2, 3};
- Ereturn new int[][]{{1, 2, 3}, {2, 4, 5}};
Information
- Description
- CS 110 Quiz on CS 110 Multiple Choice Chapters 1-8, created by Phil McCallion on 04/07/2017.
- Author
- Phil McCallion
- Language
- English
- Views
- 10
- History
- Created on 4 Jul 2017 · updated on 4 Jul 2017
- Tags
- Rating
- No ratings yet Rate this quiz