CSE114 Practice Midterm 1 PDF

Title CSE114 Practice Midterm 1
Course Computer Science I
Institution Stony Brook University
Pages 12
File Size 153.9 KB
File Type PDF
Total Downloads 40
Total Views 140

Summary

Download CSE114 Practice Midterm 1 PDF


Description

CSE 114: Computer Science I – Practice Midterm Exam #1 Name: _____________________________________________ Stony Brook ID #: _________________________________ Directions: x You have 80 minutes to complete this exam. x Do not start the exam until directed to do so. x Write all answers neatly in the space provided. x Do not separate exam sheets. No additional exam sheets will be provided. x An exam paper missing any sheets or portions of sheets will not be graded. This examination is a closed-book exam: you are not allowed to use the textbook, lecture notes, cheat sheets or anything else. Consultation of any kind of electronic device during the exam, including a cell phone, will be treated as cheating. Your exam paper will be taken and you will be charged with academic dishonesty. Use well-named variables and include any documentation where you feel it is necessary to understand your algorithms.

CSE 114 Practice Midterm Exam #1

1

1. If you attempt to add an int, a byte, a long, and a double, the result will be a __________ value. A. double B. byte C. long D. int E. float

2. What is the value of 25 % 7?

3. Circle the line or lines given below that would add number to sum. A. number += sum; B. number = sum + number; C. sum = sum + number; D. sum += number; E. sum = number + sum;

4. Given that the Unicode for character 'A' is 65, what would be the output of the following statement: System.out.print('A' + 1);

5. In Java, the word true is ________. A. a Boolean literal B. same as value 0 C. a Java keyword D. same as value 1 E. Any positive integer

CSE 114 Practice Midterm Exam #1

2

6. Suppose a program had the variable declarations shown below on the left. Draw a circle around the assignment statements below on the right that would cause compilation errors. Assume that all of the assignment statements follow the declarations. short A = 0;

F = D;

float B = 0;

D = A;

long C = 0;

E = C;

int D = 0;

F = E;

double E = 0;

C = D;

char F = 0;

D = B;

7. What is the output of the following program fragment? System.out.println('M' - 'D');

8. Write a statement that would store a random integer in the range 15-25 (inclusive) in a variable named rand.

9. You are given integer values sum and count. Write code to store the floating-point quotient of sum divided by count in a double variable named quotient.

10. Write a statement that would declare and create an array of characters of size 200 and assign it to a variable named letters.

CSE 114 Practice Midterm Exam #1

3

11. Circle the Boolean expressions below that exhibit incorrect Java syntax. A. (true) && (3 => 4) B. (x != 0) || (x = 0) C. !(x > 0) && (x > 0) D. (x > 0) || (x < 0) E. (-10 < x < 0)

12. What is the output, if any, of the following code? char ch = 'a'; switch (Character.toUpperCase(ch)) { case 'A': System.out.print(ch); break; case 'B': System.out.print(ch); break; case 'C': System.out.print(ch); break; case 'D': System.out.print(ch); break; default: System.out.println(ch); }

13. Analyze the following code fragments: Code 1: int number = 45; boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: int number = 45; boolean even = (number % 2 == 0); Which of the following statements correctly characterizes Code 1 and Code 2? A. Only Code 1 has one or more syntax errors. B. Only Code 2 has one or more syntax errors. C. Both Code 1 and Code 2 have one or more syntax errors. D. Both Code 1 and Code 2 are syntactically correct, but contain one or more logic errors. E. Both Code 1 and Code 2 are syntactically and logically correct.

CSE 114 Practice Midterm Exam #1

4

14. Analyze the following code: if (x < 100) & (x > 10) System.out.println("x is between 10 and 100"); Which of the following statements correctly characterizes the above code fragment? A. The statement has syntax errors because (x 10) must be enclosed inside parentheses. B. The statement has syntax errors because (x 10) must be enclosed inside parentheses and the println( ) statement must be put inside a block. C. The statement is syntactically correct, but has a run-time error. D. The statement is syntactically correct, but has a logical error. E. The statement is syntactically and logically correct.

15. What is the value saved in the variable count after the following code is executed? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 9); System.out.println(count);

16. Rewrite the code in the previous question to use a for-loop instead of a do-loop.

CSE 114 Practice Midterm Exam #1

5

17. Write a method that computes the following series and returns the sum as a double. The value of i should be taken as a parameter to the method. 𝑚(𝑖) =

1 2 𝑖 + +⋯+ 2𝑖 + 1 3 5

18. Write a method that will print the numbers from 1 to n, 7 numbers per line, where n is an integer parameter to the method. The numbers are separated by one space.

CSE 114 Practice Midterm Exam #1

6

19. Write a loop that displays the following pattern. 12345678987654321 234567898765432 3456789876543 45678987654 567898765 6789876 78987 898 9

CSE 114 Practice Midterm Exam #1

7

20. Write a static method buildHistogram that that accepts an array of integers in the range 0 through 100 (inclusive) as its sole parameter and that returns an array of 10 integers that provides a histogram of the values in the input parameter array. Specifically, each element of the returned array is a count of how many values of the input array fall into the ranges 0-9, 10-19, 20-21, …, 90-100, respectively. For example, suppose the parameter integer array contained the values { 5, 88, 6, 17, 39, 31, 100, 99, 19, 4, 98, 22, 77, 10 }. Then, the returned array would contain the 10 values { 3, 3, 1, 2, 0, 0, 0, 1, 1, 3 }.

CSE 114 Practice Midterm Exam #1

8

21. Write a static method stripDigits that accepts a String parameter and returns a copy of the input string after removing all digit characters from the string. Consider any special cases that might arise with the input string.

CSE 114 Practice Midterm Exam #1

9

Solutions 1. A 2. 4 3. C, D 4. 66 5. A 6. F = D; F = E; D = B; 7. 9 8. int rand = (int)(Math.random()*11 + 15); 9. average = (double)sum/count; 10. char[] letters = new char[200]; 11. A, B, C 12. a 13. E 14. A 15. 10 16. int count = 0; for (; count...


Similar Free PDFs