CmSc 150: Mid Term Exam - SOLUTIONS The first five problems check your very basic knowledge of Java. If you make more than 5 errors, your grade cannot be more than C+, no matter how you have solved the remaining problems. Be very careful! 1. Are the following numbers integers in C++ (check the appropriate box): Yes 578.0 0.0 6,789 -86 0,89 33% No X X X 3495 67. -16 58 -456. +999 X X X Yes X No X X X X X 2. Which of the following are acceptable variable names in Java: Yes No true num Yes X value3 X X To-day Figure.02 X TaxRate No X X Figure_2 X Total Sum X table_2 X Welcome! X SUM X street X fig_2_3 X intNumber X integer X _sum_ X 3. Which of the following are valid assignment statements (check for statement syntax and for variable names syntax): num = sum + 5; value = 8*sum.; yX = 68 ff = 8 + name; Yes X No X X X Yes X + y = z; 5 = x - y; if = x + 9; 9x = 6x + 3x; No X X X X 1 Multiple choice 1. A Java program is best classified as a) hardware b) software c) storage d) processor e) input 2. Of the following components of a computer, which one performs computations? a. input device b. output device c. arithmetic/logic unit d. control unit e. memory unit 3. Of the following components of a computer, which one fetches the next instruction from RAM during program execution? a. input device b. auxiliary storage device c. arithmetic/logic unit d. control unit e. memory unit 4. 5 bits can be used to represent ___ distinct items or values a) 6 b) 20 c) 24 d) 32 e) 64 2 5. Circle the proper type of the following constants: 6.7 int double char boolean String 6 int double char boolean String 6. int double char boolean String true int double char boolean String false int double char boolean String '6' int double char boolean String "6" int double char boolean String 6. What is the value of the variable a3 after execution of the following code: String a1, a2, a3; a1 = “45”; a2 = ”31”; a3 = a2 + a1; a. 45 b. 31 c. “4531” d. “3145” e. 76 7. What value will z have if we execute the following assignment statement? double z = 5 / 10; a) 0.0 b) 0.5 c) 5.0 d) 0.05 e) none of the above, a run-time error arises because z is a double and 5 / 10 is an int 3 8. If x is an int and y is a float, all of the following are legal except which assignment statement? a) y = x; b) x = y; c) y = (float) x; d) x = (int) y; e) all of the above are legal 9. Which of the following is true regarding the mod operator, %? a) It can only be performed on int values and its result is a double b) It can only be performed on int values and its result is an int c) It can only be performed on float or double values and its result is an int d) It can only be performed on float or double values and its result is a double e) It can be performed on any numeric values, and the result always is numeric 10. Assume that x, y, and z are all ints equal to 50, 20, and 6 respectively. What is the result of x / y / z? a) 0 b) 12 c) 16 d) A syntax error as this is syntactically invalid e) A run-time error because this is a division by 0 11. The value of the Java expression a. 0.0 b. 0 c. 7.5 d. 5 e. 0.5 6 / 4 * 5 is: 4 12. Given that x is a double variable and num is an int variable containing the value 5, what will x contain after execution of the following statement: x = num / 2; a. 2 b. 2.0 c. 2.5 d. 5.0 e. nothing; a compile-time error occurs 13. Given that x is a double variable containing the value 1.5 and num is an int variable, what will num contain after execution of the following statement: num = x + 2; a. 3.5 b. 4 c. 3 d. 3.0 e. nothing; a compile-time error occurs 14. If the int variables int1 and int2 contain the values 5 and 2, respectively, then the value of the expression (double)(int1) / int2 is: a. 0.25 b. 0 c. 2.0 d. 2.5 e. 2 15. If the int variables int1 and int2 contain the values 5 and 2, respectively, then the value of the expression (double)(int1 / int2 ) is: a. 0.25 b. 0 c. 2.0 d. 2.5 e. 2 5 16. Circle the value of each expression in the column below, given the following values for x, y, and z: int x, y , z; x = 7; y = 6; z = 3; EXPRESSION VALUE x < y True False x + 2 < y True False x != y True False x + 3 >= y True False y == x True False y < x || y < z True False x != y && y < z True False x == z || x > y True False !(true && false) True False 17. After execution of the following code, what will be the value of item if its initial value is 10? if (item > 5) item = item + 5; if (item < 10) item = item + 10; else if (item < 20) item = item + 10; a. 0, b. 5, c. 10, d. 15, e. 25 18. After execution of the following code, what will be the value of item if its initial value is 10? if (item > 5) item = item + 5; else if (angle < 10 ) item = item + 10; a. 0, b. 5, c. 10, d. 15, e. 25 6 19. Consider the following switch statement where x is an int, switch (x) { case 3 : y = x + 1; case 4 : x = x + 2; y = ++x; case 5 : x = y + 3; y = x++; break; case 6 : x++; y = x; } If x is currently equal to 4, what will the value of y be after the switch statement executes? a) 6 b) 7 c) 8 d) 10 e) 11 20. If x is an int where x = 0, what will x be after the following loop terminates? while (x < 100) x = x*2; a) 2 a) 64 b) 100 c) 128 d) None of the above, this is an infinite loop 7 21. Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates? for (int i = 0; i < 3; i++) x = x + i; a) 0 b) 1 c) 3 d) 5 e) 10 22. The do–while loop differs from the while loop in that a) the while loop will always execute the body of the loop at least once b) the do–while loop will always execute the body of the loop at least once c) the do–while loop will continue to loop while condition in the while statement is false and the while loop will continue to loop while the condition in the while statement is true d) the while loop will continue to loop while condition in the while statement is false and the do–while loop will continue to loop while the condition in the while statement is true e) none of the above, there is absolutely no difference between the two types of loops 23. How many times will the following loop iterate? int x = 10; do { System.out.println(x); x--; } while (x > 0); a) 0 times b) 1 time c) 9 times d) 10 times e) 11 times 8 24. What is the value of loopCount after control exits the following loop? loopCount = 1; while (loopCount < 100) { alpha = alpha + 7; loopCount++; } a. 1, b. 99 c. 100 d. 101 25. What is the value of someInt after control exits the following loop? someInt = 3; for (k = 0; k < 3; k++) someInt = someInt * k; a. 0 b. 2 c. 5 d. 6 e. none of the above--this is an infinite loop True/False Questions: 1. F The word “Public” is a reserved word. 2. T The following for-loop is an infinite loop. for (int j = 0; j < 1000; ) i++; 3. T The two components of the central processing unit (CPU) are the arithmetic/logic unit and the control unit. 4. T Integer values and floating-point values are stored differently inside the computer. 5. F The expression !(n <= 5) !(n == 5 && n < 5) is equivalent to the expression 6. T In the Java program fragment count = 1; while (count < 10); count++; 9 the statement count++ is not part of the body of the loop. 7. F It is possible for the body of a do - while statement never to be executed. 8. T It is possible for the body of a for statement never to be executed. 9. T It is possible for the body of a while statement never to be executed. 10. F The termination condition for the while loop int sum = 0, loopCount = 1; while (loopCount < 9) { sum = sum + loopCount; loopCount++; } is loopCount > 9. Fill in the Blanks: 1. _______machine language_______ is the language made up of binary-coded instructions that are used directly by the computer. 2. A(n) ____compiler____________ is a program that translates a high-level language program into machine code. 3. A(n) ____algorithm_________ is a step-by-step procedure for solving a problem in a finite amount of time. 4. A(n) ___constant __________ is a location in memory, referenced by an identifier, where a data value that cannot be changed is stored. 5. A(n) __assignment_________ is a statement that stores the value of an expression into a variable. 6. The set of rules that determines the meaning of instructions written in a programming language is called ___semantics_________________. 7. ____Syntax___________ is the formal rules governing how valid instructions are written in a programming language. 10 Free Answers 1. A truth table shows, for the various true or false values of boolean variables, what the result of a boolean condition is. Write the value of the expression a && (!b | | c) for the given values of a, b, and c. Assume that a, b and c are boolean variables. a b c a && (!b false false false false true false false true true true false false || c) 2. Given two variables x and y, write one statement that will store x into y and increment x. For example, if x is 5, after the execution y will be 5 and x will be 6. y = x++; 3. Write statements to perform the following computation: If any of x and y is at least 100 and their product is at most 1000 , store their product into z. Otherwise store 1000 into z. if ((x >= 100 || y >= 100) && x*y <= 1000) z = x*y; else z = 1000; 11 4. In the code fragment if (a >= 10) if (a < 20) a = a + 2; else a = a + 1; what would be the value of a in the following cases Initial value value of a after execution ---------------------------------------------------------------5 5 10 12 15 17 20 21 25 26 5. Write a program fragment that finds the sum of the integers 1 through 10 int sum = 0; for (int k = 0; k < 11; k++) sum = sum + k; 12