Вы находитесь на странице: 1из 4

n

ve
gi
be
.
ll one t
i
w e le ork
k
m
w
ar o ou r
m py s r y ou
y
o
r co k o y
p
r
ze
COMP112/14-T01b Programming I
A you wo co
e
if ses on
el me
Test #1 (b)
so

2014-10-24
Due Date In Class

Class Code

WRITE YOUR NAME


DO NOT

Student No.

This is a CLOSED BOOK test, 80 minutes, 100 full marks.

1. What is a bit ?

A binary digit
8

How many bytes does a Java long integer contain?

(1)

(2)

(3)

The fast storage cells in a CPU to hold operands and intermediate results are called:

registers
2. The Java compiler translates

Java source

(4)

Java class

files to

(5)

files.

How to state the external classes that your program refers to?

Use the import statement

(6)

3. Identify the errors in the following code:


1
2
3
4
5
6

Public class Wrong {


static void main(STRING[] args) {
x = 100;
System.out.println("Result: + x)
}
}

1) Line 1:
2) Line 2:
3) Line 2:
4) Line 3:
5) Line 4:
6) Line 4:

Public should be public (lowercase).


STRING should be String.
The main method should be declared public.
x should be declared with a type: int x.
the string literal should be closed by double quotation marks.
the terminating semicolon missing.
. 6
(7)

4. Suppose a Scanner object is created as follows:


Scanner sc = new Scanner(System.in);
The following code keeps reading numbers until it reads a negative one. Complete the code.
double a =

sc.nextDouble()

(8)

while ( a >= 0 )

a =

sc.nextDouble()

(9)

1/4

5. Which of the following are correct Java identifiers?


String

class

9ABC

_12_

100/10

MAX

float

String, _12_, MAX

6. What is the exact output of the following code?

number1020

(10)

(11)

(12)

(13)

(14)

int number = 10;


System.out.print("number"+10);
System.out.print(number+10);

7. If you enter 1 2 3 4, when you run this program, what will be the output?
import java.util.Scanner;
public class Four {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter four numbers: ");
int n1 = input.nextInt(), n2 = input.nextInt();
int n3 = input.nextInt(), n4 = input.nextInt();
double average = (n1 + n2 + n3 + n4) / 4;

System.out.println(average);
}
}

2.0
8. Write a statement to declare a constant STANDARD_NAME with value "Name".

final String STANDARD_NAME = "Name";


9. Write out the results of the following expressions.
246 % 5 evaluates to:

77.0 / 2 evaluates to:

38.5

(15)

-50 / -4 evaluates to:

12

(16)

-50 % -4 evaluates to:

-2

(17)

10. Consider the following code.


public class Hour {
public static void main(String[] args) {
int hour = 010;
System.out.println("the hour is " + hour);
}
}

Determine whether the program is correct, if correct, write out the result, otherwise, point out
what the error is.

The program prints: the hour is 8.

(18)

2/4

MPI.COMP112/14.T01b

30

11. Suppose a is an integer variable, the following evaluates to:

(19)

(20)

(21)

(22)

(23)

(24)

(25)

(26)

(27)

(a = 10) + 20 / (9 - 5) * 4

12. Suppose z is 100. What is z after z += z*100 ?


13. What is x after the following statements?

10100

int x = 2, y = 1;
x *= x + y++ + 1;

14. Which of the following expressions are the same in terms of both side-effects and results?
(A) z = z+1

(B) ++z

(C) z++

(A) and (B) are the same


15. What is the value of i printed in the following code?

public class Test {


public static void main(String[] args) {
int j = 0, k = 1;
int i = j++ + --k * 10;
System.out.println("What is i? " + i);
}
}

16. To assign an int variable i to a char variable c , you write

c = (char)i;
17. The following code prints:

1.5

char a = 'A', b = 'B'; int c = 3; double d = 1.0;


System.out.println((b-a+c-d)/2);

18. Write a statement to declare a long integer variable n and initialize it to 123456789000.

long n = 123456789000L;

19. What will be displayed by System.out.println('9'-'7')?

20. Write an expression to determine whether variable x is greater than 100 and is a multiple of 4.

x > 100 && x % 4 == 0


21. In Java, is the word false a Java keyword or a Boolean literal?

a Boolean literal

(28)

(29)

(30)

(31)

22. Write a conditional expression to compute the minimum of variables a , b , c .

a < b && a < c ? a : b < c ? b : c


23. To check whether a character variable ch is a lowercase letter, you write:

(ch >= 'a'&& ch <= 'z')


3/4

24. Suppose income is 20001. Will anything be displayed by the following statement? If yes, what
will that be?
if (income > 10000)
System.out.println("Income is greater than 10000");
else if (income > 20000)
System.out.println("Income is greater than 20000");

Yes. Income is greater than 10000.


10

25. Suppose x = 10 and y = 10. What is x after the following expression?

(32)

(33)

(34)

(y > 10) && (x-- > 10)

50

26. How many times will the following code print "Hello world!"?
int count = 50;
while ( count > 0 ) {
System.out.println("Hello world!");
count--;
}

27. The following code prints all the digits in a string. Complete the code.
for ( int i = 0; i < s.length();
char c =

++i

(35)

s.charAt(i)

(36)

if ( '0' <= c && c <= '9' )

) {

System.out.print(c);

(37)

28. How many times is the println statement below executed?

300

(38)

for ( int i = 0; i < 25; i++ )


for ( int j = 0; j < i; j++ ) System.out.println(i * j);

29. The following computes this series for 100 items and stores the result in s . Complete the code.
1 + 3 + 7 + 13 + 21 + 31 + 43 +
int s =

(39)

, t =

(40)

, d =

(41)

for ( int i = 0; i < 100; ++i ) {

s = s+t; t =

t+d

(42)

; d =

d+2

(43)

30. What pattern is produced by the following code?


for (int i = 1; i <= 5; ++i) {
for (int j = 5; j >= 1; --j)
System.out.print(j <= i ? j + " " : "
System.out.println();
}

2
3 2
4 3 2
5 4 3 2

1
1
1
1
1

");

(44)

4/4

Вам также может понравиться