Sie sind auf Seite 1von 12

CS180 Fall 2013 Midterm 1 exam September 26th

Version A
PART1 Multiple choice questions

1. Assume a paragraph of text contains around 500 characters. Approximately how


much memory will it consume when stored in a Java program:
A.
B.
C.
D.
E.

50 bytes
1 KB (**)
1 MB
1 GB
1 TB

2. Binary is a way of encoding:


A.
B.
C.
D.
E.

characters
integers
floating point numbers
references
all of the above (**)

1|Page

CS180 Fall 2013 Midterm 1 exam September 26th


Version A
3. What is -42 decimal expressed in 8-bit twos-complement binary?
A.
B.
C.
D.
E.

11010110 (**)
11010111
11010101
11011000
11010100

4. The highest (signed) positive integer that can be represented in n-bit twoscomplement binary is:
A.
B.
C.
D.
E.

n
n-1
2n-1-1 (**)
2n-1
2n-1

2|Page

CS180 Fall 2013 Midterm 1 exam September 26th


Version A
5. The lowest (signed) negative integer that can be represented in n-bit twoscomplement binary is:
A.
B.
C.
D.
E.

-n
-n-1
-2n-1-1
-2n-1 (**)
-2n-1

6. What does the following Java code snippet print to standard output?
byte i = 127;
i++;
System.out.println(i);
A.
B.
C.
D.
E.

127
128
-127
-128 (**)
none of the above

3|Page

CS180 Fall 2013 Midterm 1 exam September 26th


Version A
7. What does the following Java code snippet print to standard output?
byte i = -128;
i--;
System.out.println(i);
A.
B.
C.
D.
E.

127 (**)
128
-127
-128
none of the above

8. Which of the following is true?


A.
B.
C.
D.
E.

Classes and objects are synonyms


Many classes can be created from an object
Many objects can be created from a class (**)
An object is a class with extra fields
A class is an object with extra fields

4|Page

CS180 Fall 2013 Midterm 1 exam September 26th


Version A
9. Which of the following is false?
A. A compiler is a program
B. An interpreter is a program
C. A compiler translates a program written in a high-level programming language
into an equivalent program in machine language
D. An interpreter executes a program written in a high-level programming
language
E. A compiler executes a program written in a high-level programming language
(**)

10. Which of the following is not a programming abstraction in Java?


A.
B.
C.
D.
E.

classes
objects
variables
methods
comments (**)

5|Page

CS180 Fall 2013 Midterm 1 exam September 26th


Version A

11. Which Java class can read from the standard input?
A.
B.
C.
D.
E.

System
Scanner (**)
Math
String
Character

12. What does the following code snippet print to standard output?
final double PI=3.14159;
System.out.printf(%.3f\n, PI);

A.
B.
C.
D.
E.

3.141592653589793
3.14159
3.142 (**)
3.141
3.000

6|Page

CS180 Fall 2013 Midterm 1 exam September 26th


Version A
13. Which of the following declarations is illegal?
A.
B.
C.
D.
E.

int
int
int
int
int

i;
i =
i =
i =
i =

0;
0;
-0;
0.0; (**)

14. Which of the following statements allocates an array of 100 int values?
A.
B.
C.
D.
E.

int a[100];
int[] a = {100};
int[] a = new int[100]; (**)
int[] a = new int[]{100};
all of the above

7|Page

CS180 Fall 2013 Midterm 1 exam September 26th


Version A
15. Which of the following code snippets is not equivalent to x = a || b && c?

A.
B.
C.
D.
E.

if (a)
x = a;
x = a;
x = a;
x = a;

x = true; else if (b) x


if (!x) x = b; if (x) x
if (!x) if (b) x = c;
if (x) {} else if (b) x
if (!x) { x = b; if (x)

= c; else x = false;
= c; (**)
= c;
x = c; }

16. In the following code snippet, which statements get executed?


int i = 1;
switch (i) {
case 1:
statement 1;
case 2:
statement 2;
break;
case 3:
statement 3;
default:
statement 4;
break;
}

A.
B.
C.
D.
E.

statement
statement
statement
statement
statement

1;
2;
1; and statement 2; (**)
1; and statement 4;
1;and statement 2; and statement 4;

8|Page

CS180 Fall 2013 Midterm 1 exam September 26th


Version A
17. In the following code snippet, how many iterations of the loop execute?
int i = 0;
while (i < 5) {
if (i != 0)
i = i + 1;
else
i = i + 3;
}
A.
B.
C.
D.
E.

infinitely many
2
3 (**)
4
5

18. The following incomplete code snippet is intended to compute the sum of all the
elements of the array.
public int sum(int[] array) {
int sum = 0;
int i = 0;
for (int elt : array) {
// missing loop body
}
return sum;
}
Which of the following loop bodies correctly computes the sum?
A.
B.
C.
D.
E.

sum +=
sum +=
sum +=
both a
both b

elt;
array[i++];
array[++i];
and b (**)
and c

9|Page

CS180 Fall 2013 Midterm 1 exam September 26th


Version A
19. Using the final modifier on a variable declaration makes that variable:
A.
B.
C.
D.
E.

no longer usable
unable to be modified after it is initialized (**)
automatically initialized to zero
usable only in loops
initialized to null

20. Which expression is false?


A.
B.
C.
D.
E.

"hello".charAt(0) == 'h'
"HELLO".toLowerCase() == "hello" (**)
"hello".indexOf('o') == 4
"HELLO".substring(2, 4).equals("LL")
hello == hello

10 | P a g e

CS180 Fall 2013 Midterm 1 exam September 26th


Version A
PART2 Programming questions
PQ1 (weight 30%)
Write a method with signature int median(int[] array) that accepts an array of
integers as argument and returns the median integer in the array (the value that
separates the lowest half of the values in the array from the highest half). You may use
the method static void sort(int[] a) of the java.util.Arrays class as
necessary, but make sure you do not perturb the original array in any way (consider
using the method static int[] copyOf(int[] a) of the java.util.Arrays
class as necessary).

ANSWER
int median(int[] array) {
int[] a = Arrays.copyOf(array);
Arrays.sort(a);
return a[(int)(a.length / 2)];
}

11 | P a g e

CS180 Fall 2013 Midterm 1 exam September 26th


Version A
PQ2 (weight 20%)
Write a program that prints the prime numbers between 1 and 100. A prime number is a
number that is divisible only by 1 and itself. Thus, in your program there is no need to
test for divisibility by 1, nor for divisibility by numbers greater than or equal to itself.
ANSWER:
class Primes {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
boolean prime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
prime = false;
break;
}
}
if (prime)
System.out.println(i);
}
}
}

12 | P a g e

Das könnte Ihnen auch gefallen