student name [ ]
American University of Central Asia
Software Engineering Department
Programming I (COM 116), Structural Programming (COM-118)
Sample Midterm Examination
1. What is the name of the component that 8. Who was the designer of the Java pro- d) public static void main(String[]
executes program instructions? gramming language? args)
a) CPU a) Bjarne Stroustrup 16. Which of the following are valid Java
comments?
b) Memory b) James Gosling
c) Bus c) Dennis Ritchie a) // This will compile
d) Disk d) John McCarthy b) /* This will compile */
c) /** This /// will compile */
2. How many bits are there in a byte? 9. What should be the command to compile d) /* /* This will compile */ */
a Java source file [Link] into a pro-
a) 1 gram? Select the types of the programming er-
b) 2 rors in the following situations (questions
a) java [Link] 16–18).
c) 4
b) java [Link]
d) 8 17. Situation #1: It is not possible to compile
c) javac [Link] a program.
3. Which of the following are storage de- d) javac Main
a) Compilation error
vices?
10. The Java compiler transforms Java b) Runtime error
a) Solid-state drive sources into [ ]? c) Logic error
b) Hard disk drive
a) Machine code 18. Situation #2: The program does not pro-
c) USB flash drive duce a correct answer.
b) Assembly language
d) Blu-ray disc c) Java bytecode a) Compilation error
4. Select a list of types of memory ordered d) C source code b) Runtime error
correctly from fastest to slowest. c) Logic error
11. Central processing units can only execute
a) Memory / CPU registers / Disk [ ]? 19. Situation #3: The program crashes dur-
drive ing execution.
a) Java source code
b) Memory / Disk drive / CPU regis- a) Compilation error
ters b) C source code
b) Runtime error
c) CPU registers / Memory / Disk c) Machine code
c) Logic error
drive d) Assembly language
d) Disk drive / Memory / CPU regis- 20. Which of the following are valid identi-
ters 12. What should be the command to start a fiers?
Java program from a file [Link]?
5. What is the name of the program that a) $2732
manages hardware resources and pro- a) java [Link] b) 2x
grams on a computer? b) java Main c) true
c) javac [Link] d) value
a) Compiler
d) javap [Link] e) 1+2
b) IDE
c) JDK 13. Is it possible to run the same compiled 21. Which of the following are correct ways
Java program on machines with different to declare variables?
d) Operating system
types of CPUs?
e) Debugger a) int length, int width;
a) Yes b) int length; int width;
6. What is the name of the company that
b) No c) int length, width;
created the Java programming language?
d) int length; width;
14. Is it possible to run the same compiled
a) Microsoft
Java program on machines with different 22. What is the assignment operator in Java?
b) Google operating systems?
a) ==
c) Sun Microsystems
a) Yes b) !=
d) Oracle
b) No c) =
7. Who is the current owner and maintainer d) :=
15. How should a correct entry point of a Java
of the Java language?
program look like? 23. Which of the following assignment state-
a) Oracle ments is incorrect?
a) public static void Main(string[]
b) Sun Microsystems args) a) i = j = k = 1;
c) Apple b) public void main(String[] args) b) i = 1; j = 1; k = 1;
d) Intel c) public static int main(String[] args) c) i = 1 = j = 1 = k = 1;
24. According to Java naming convention, c) 5 41. Suppose x = 1, y = -1, and z = 1. What
which of the following names can be vari- will be displayed by the following state-
d) 6
ables? ment?
a) isOdd 33. What will be displayed by the following i f ( x > 0)
code? i f ( y > 0)
b) Main System . out . p r i n t l n ( " I " ) ;
c) length double x = 5 . 5 ; else i f ( z > 0)
int y = ( int ) x ; System . out . p r i n t l n ( " I I " ) ;
d) PI System . out . p r i n t l n (
e) false "x i s " + x + a) I
" and y i s " + y b) II
25. Which of the following is a constant, ac- );
cording to Java naming conventions? c) Nothing
a) x is 5 and y is 6
a) radius 42. Suppose isPrime is a boolean variable,
b) x is 6.0 and y is 6.0 which of the following is the correct and
b) pi
c) x is 6 and y is 6 best statement for testing if isPrime is
c) MAX_FORCE true.
d) x is 5.5 and y is 5
d) Main
e) x is 5.5 and y is 5.0 a) if (isPrime = true)
26. Which of these data types requires the b) if (isPrime == true)
most amount of memory? 34. What is the value of (double) (5 / 2)? c) if (isPrime)
a) long a) 0.0 d) if (isPrime != false)
b) short b) 2.0 43. What is the output of the following code?
c) int c) 2.5 int a = 0 ;
d) byte i f ( a < 4) {
d) 2
a += 1 ;
27. What is the result of 40 / 3 ? e) 3 }
System . out . p r i n t l n ( a ) ;
a) 13.333 35. What is the value of (double) 5 / 2 ?
b) 13.(3) a) 0
a) 0.0 b) 1
c) 10
b) 2.0 c) 2
d) 13
c) 2.5 d) The code will not compile
28. What is the result of 40 % 3 ?
d) 2
44. Analyze the following code
a) 0 e) 3
// I
b) 1
36. Which of the following assignment state- i n t number = 4 2 ;
c) 0.(3) ments are illegal? boolean odd ;
d) 0.333
a) float x = -42; i f ( number % 2 != 0 )
29. Which of the following are the same as odd = true ;
b) int x = 42;
1248.23453 ? else
c) int x = 4.2; odd = f a l s e ;
a) 1.24823453e+3
d) int x = (int) true;
b) 0.124823453e+4 // I I
e) short x = 2; i n t number = 4 2 ;
c) 124823453.0e-3 boolean odd = ( number % 2 != 0 ) ;
d) 124823453.0e-5 37. What is the equal comparison operator in
Java? a) Code (I) will not compile.
30. The expression 2 + 10 / (4 - 1) * 5 is b) Code (II) will not compile.
evaluated to a) =
c) Both will not compile
b) <>
a) 20 d) Both are correct, but code (II) is
b) 17 c) == better
c) 0 d) !=
45. Suppose income is 2001, what is the out-
d) 25 e) ^= put of the following code:
31. What is x after the following statements? 38. What is 1 + 1 + 1 + 1 + 1 == 5 ? i f ( income > 1 0 0 0 ) {
System . out . p r i n t l n ( " >1000" ) ;
int x = 3 ; a) false } e l s e i f ( income > 2 0 0 0 ) {
int y = 4 ; System . out . p r i n t l n ( " >2000" ) ;
x ∗= y + 1 ; b) true
}
c) There is no guarantee that it is true
a) 0 a) no output
b) 12 39. What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == b) >1000
5.0 ?
c) 5 c) >2000
d) 15 a) false d) >1000 and >2000
b) true 46. Which of the following is the correct ex-
32. What is the value of i printed in the fol-
lowing code? c) There is no guarantee that it is true pression that evaluates to true if the num-
ber x is between 1 and 100 or the number
int j = 0 ; 40. In Java programming language, the word is negative?
i n t i = j++ + j ∗ 5 ; true is [ ].
a) 1 <x <100 && x <0
System . out . p r i n t l n ( i ) ; a) a Boolean literal b) (1 >x >100) || (x <0)
a) 0 b) same as value 1 c) ((x <100) && (x >1)) || (x <0)
b) 1 c) same as value 0 d) ((x <100) && (x >1)) && (x <0)
47. What will be displayed by the following 53. What is the output of the following code? // C
switch statement? f o r ( i n t i = 0 ; i <= 1 0 0 ; i ++) {
int i = 0 ;
System . out . p r i n t l n ( " Hi " ) ;
char c = ’ a ’ ; while ( i < 1 0 ) {
}
switch ( c ) { i = i + 1;
case ’ a ’ : }
// D
case ’A ’ : System . out . p r i n t l n ( i ) ;
f o r ( i n t i = 1 ; i < 1 0 0 ; i ++) {
System . out . print ( c );
a) 0 System . out . p r i n t l n ( " Hi " ) ;
break ;
b) 1 }
case ’ b ’ :
case ’B ’ : c) 9 a) A and B
System . out . print ( c );
d) 10 b) C and D
break ;
case ’ c ’ : 54. What will be displayed when the follow- c) A and C
case ’C ’ : ing code is executed?
System . out . print ( c ); d) B and C
break ; int x = 1 0 ; e) A and B and C
case ’ d ’ : while ( x > 0 ) {
case ’D ’ : x −= 4 ; 59. Do the following two loops in (I) and (II)
System . out . print ( c ); System . out . p r i n t ( x + " " ) ; result in the same value in sum?
} }
// I
a) abcd a) 6 2 i n t sum = 0 ;
b) ab b) 6 2 -2 f o r ( i n t i = 0 ; i < 4 2 ; ++i ) {
c) 10 6 4 sum += i ;
c) a
}
d) b d) 10 6 4 -2
e) c // I I
55. How many times will the following code
f) d i n t sum = 0 ;
print "Hi"?
f o r ( i n t i = 0 ; i < 4 2 ; i ++) {
48. What is y after the following statement is int i = 0 ; sum += i ;
executed? do { }
System . out . p r i n t l n ( " Hi " ) ;
x = 0; } while ( i++ < 1 0 0 ) ; a) No
y = ( x > 0 ) ? 42 : −42;
a) 0 b) Yes
a) 42
b) 99 60. How many times is the println statement
b) -42
c) 100 executed?
c) 0
d) 101
d) Will not compile f o r ( i n t i = 0 ; i < 1 0 ; i ++) {
f o r ( i n t j = 0 ; j < i ; j ++) {
56. How many times will the following code
49. The order of the precedence (from high to System . out . p r i n t l n ( i ∗ j ) ;
print "Hi"?
low) of the operators binary +, *, &&, || }
is int i = 0 ; }
do {
a) &&, ||, *, + System . out . p r i n t l n ( " Hi " ) ; a) 10
b) *, +, &&, || } while (++ i < 1 0 0 ) ; b) 100
c) *, +, ||, && a) 0 c) 55
d) ||, &&, *, + b) 99 d) 45
50. What is the value of the following expres- c) 100
sion? d) 101 61. What is sum after the following loop ter-
minates?
true | | true && f a l s e 57. Analyze the following code
i n t sum = 0 ;
a) true double sum = 0 ; int i = 0 ;
b) false f o r ( double d = 0 ; d < 1 0 ; ) { do {
d += 0 . 1 ; i ++;
51. How many times will the following code sum += sum + d ; sum += i ;
print "Hi"? } i f ( sum > 4 ) { break ; }
} while ( i < 5 ) ;
int i = 0 ; a) The program has a compile error be-
while ( i < 1 0 0 ) { cause the adjustment is missing in a) 4
System . out . p r i n t l n ( " Hi " ) ; the for loop.
i ++; b) 5
b) The program has a compile error be-
} cause the control variable in the for c) 6
a) 1 loop cannot be of the double type. d) 7
b) 10 c) The program runs in an infinite loop
because d <10 would always be true. 62. What is sum after the following loop ter-
c) 100
d) The program compiles and runs fine. minates?
d) 99
i n t sum = 0 ;
58. Which of the following loops prints "Hi" int i = 0 ;
52. How many times will the following code
100 times? do {
print "Hi"?
// A i ++;
int i = 0 ; sum += i ;
f o r ( i n t i = 0 ; i < 1 0 0 ; i ++) {
while ( i++ < 1 0 0 ) { i f ( sum >= 4 ) { continue ; }
System . out . p r i n t l n ( " Hi " ) ;
System . out . p r i n t l n ( " Hi " ) ; } while ( i < 5 ) ;
}
}
a) 1 // B a) 14
b) 10 f o r ( i n t i = 1 ; i <= 1 0 0 ; i ++) { b) 15
System . out . p r i n t l n ( " Hi " ) ;
c) 100 c) 16
}
d) 99 d) 17