CS: answer quick study guide questions.

profileSaerii133
midtermpreview-f-16.pdf

1

Midterm Preview Time allotted: 50 minutes

CS 110, Programming Fundamentals I

Central Washington University

Computer Science

Instructions

There are five sections on this exam that contain 24 required questions, a sixth section that contains extra

credit questions, and a last, seventh section, with supplementary information that may be helpful in answering

some of the required questions. This midterm is worth 100 points. Each question in a section is worth the points

indicated at the beginning of the section.

• Pace yourself

• Do not spend too much time on any one question

• Partial credit will be given, if warranted

• Partial credit will NOT be given on the extra credit questions, and points will NOT be taken off for

incorrectly answering an extra credit question

This exam is a closed book, closed notes, no IPhones, no Internet, etc. exam. All that you need is a pen or

pencil.

Name (Print)

Honor Code Statement: I pledge that this submission is solely my work, and that I have neither

given to, nor received help from anyone.

Signature:

Section Question Type Question Numbers Points Possible Points Scored

I True / False 1-10 20

II Multiple Choice 11-15 20

III Find the Error 16 20

IV Short Answer 17-22 30

V One or More Answers 23-24 10

Total 100

VI Extra Credit

Total Including Extra Credit

2

Section I: True/False Each question is worth 2 points; no partial credit given

Circle either True or False.

1. True / False The following is a syntactically correct variable declaration and assignment

statement:

double int = 2.0;

2.

True / False

The diagram in Figure 1a is the decision structure logic of the Java statements in

Figure 1b.

if (Condition_1){

if (Condition_2){

Statement_B;

}else{

Statement_C;

}

}

if (! Condition_1){

Statement_A;

}

Figure 1a Figure 1b

3. True / False Java is a case sensitive programming language.

4. True / False Assuming that letter has been declared as a variable of type char, the below statement is syntactically correct:

letter = “a”;

5. True / False For the logical AND operator, &&, which connects two boolean expressions, both

expressions must be false for the overall expression to be false.

6. True / False The below two pieces of code output the same thing to the console:

int someVariable = 0;

System.out.println(“Output : “ + someVariable);

int someVar1 = 1, someVar2 = 2; System.out.println(“Output : “ + someVar1 / someVar2);

7. True / False Syntax errors are mistakes that the programmer has made that violate the rules of the

programming language.

3

8. True / False The following Java code is syntactically incorrect:

Scanner keyBoard = new Scanner(System.in); int someValue = keyBoard.nextInt();

switch (someValue){

case 1:

case 2:

case 3:

System.out.println("Input is 1 or 2 or 3.");

break; default:

System.out.println("Input is not 1, not 2 nor 3.");

}

9. True / False The following piece of Java code is syntactically correct:

int k = 0;

for (int i=0; i<10; i--);{k++;}

10. True / False The equality operator, ==, cannot be used to compare the values stored in two

variables of type String.

Section II: Multiple Choice Each question is worth 4 points; no partial credit given

Directions: Circle the single correct choice from among the provided lettered options for each question.

11. Assuming x has been declared as a variable of type int, the below Java code in the box is equivalent to which of the logical statements?

if (x > -1 && x < 100)

A. If x is greater than 0 and x is less than 99

B. If x is greater than 1 and x is less than 99

C. If x is (0 or greater) and x is less than 99

D. If x is (0 or greater) and x is at most 99

12. What does the following piece of Java code output:

String sentence = “heya”;

System.out.println(sentence.toLowerCase());

A. heya

B. Heya

C. HeYa

D. HEYA

4

13. Major components of a typical modern computer consist of:

A. The Central Processing Unit

B. Input/output devices

C. Secondary storage devices

D. All of the above

14. What will be the values of x and y after the following code is executed?

int x = 25, y = 8;

x += (7-y);

y--;

A. x = 24, y = 7

B. x = 24, y = 8

C. x = 25, y = 7

D. x = 25, y = 8

E. x = 26, y = 7

F. x = 26, y = 8

15. What would be the value of percentVal after the following statements are executed?

A. 0.05 double percentVal = 0.0; int purchase = 950;

B. 0.04 char cust = 'N';

C. 0.03 if (purchase > 1000){

D. 0.02 if (cust == 'Y') E. 0.01 percentVal = .05;

else percentVal = .04;

} else {

if (cust == 'Y')

percentVal = .03;

else

percentVal = .02;

}

percentVal = .05;

5

Section III: Find the Error This question is worth 20 points; partial credit given

16. For the below Java code, circle any portion that contains a syntax error. Circle those parts of the

Java code where there is a mistyped character, missing semicolon, etc.

public class HowMuchTimeRemaining

public static void main (String[] args){

timeRemaining double = 12.5;

timeSinceStart double = 33.0;

if (timEremaining =< 10.647320){

System.out.println("Less than 10 minutes

left.")

} else if (timeRemaining < 20.2098756434){

System("Less than 20.2098756434 but more than 10.647320 minutes left.")

} else {

System.out.println("Ahh. At least 20 minutes remaining.")

}

}

}

Section IV: Short Answer Each question is worth 5 points; partial credit given Directions: Provide a short, concise answer for each question.

17. Explain the following line of code (do NOT predict the value of x; explain what the statement does).

final int x = 10/5;

Your Answer :

18. What is the value of y after the following code is executed?

int y = 1;

while (y < 10){

y += 3;

}

Your Answer :

6

19. What is the output of the following Java Program?

public class MidtermQuestion {

public static void main (String[] args){

String cwu = "Central Washington University";

System.out.print(cwu.substring(0,1));

System.out.println(cwu.substring(cwu.length()-3,cwu.length()));

} }

Your Answer : 20. The binary encoding 00001110 is equivalent to what base-10 number? Show your work to receive

partial credit, in the case that your answer is incorrect.

Your Answer : 21. Write a one-line syntactically correct Java statement that declares a variable animal of type String

and assigns it the value tiger

Your Answer:

7

22. Write a Java program that will print to the console all even integers starting from 2 through (and

including) 50. Complete the program, which has been started for you. Hint: you can use a while or for loop. Hint 2: One correct solution requires only 3 lines of VERY simple code.

// A program that prints the even integers

// from 2 through (and including) 50

public class EvenNumFrom2Through50 {

// the main routine

public static void main (String[] args) {

}

}

8

Section V: Multiple Choice, Multiple Answer Each question is worth 5 points; partial credit given

Directions: Select ALL correct answer choices, for each question. The correct answer might be one, two-

or more, or all choices. For each choice that you circle that is NOT a correct answer, you will be deducted

1 point. For each choice that you do NOT circle that should be circled, you will lose 1 point. You cannot

lose more than 5 points for a question.

23. Which of the following statements is/are true about comments.

A. A single line comment begins with the characters \\

B. A multiple-line comment begins with */

C. The following is a syntactically correct comment: // */ */ ////////// D. Single-line comments are ignored by the compiler

E. Multiple-line comments are not ignored by the compiler

F. Multiple-line comments must end with the characters /* 24. Based on the for-loop shown below, which of the lettered choice is/are correct?

for (int i=-1; i<10; i+=2){

System.out.print(i);

}

A. The body of the for-loop will be executed 5 times

B. The i+=2 part of the for loop will be performed 5 times

C. The output produced by the for loop will be -113579

D. The i+=2 part of the for loop is performed as many times as the i<10 check is performed

E. The i<10 part of the for loop is the initialization portion, and is executed only once

F. The i+=2 part of the for loop is executed at least once.

Section VI: Extra Credit; Questions are worth 3 points; no partial credit given; no penalty for guessing

This section will contain 3 extra credit questions.

9

Section VII: Supplementary Information which MAY help you to answer some of the required questions

String Methods

Method Description

charAt(index) The argument index is an integer value and specifies a character position in the string

length() This method returns the number of characters in the string

toLowerCase() This method returns a new string that is the lowercase equivalent of the original string object

toUpperCase() This method returns a new string that is the uppercase equivalent of the original string object

substring(a,b) This method returns a substring with starting position a and continuing to (and not including)

the character at position b of the original string.

Binary Representation of Base-10 Integers Left-most Right-most

Binary representation for

each position of a byte 7 2

6 2

5 2

4 2

3 2

2 2

1 2 2

0

Decimal equivalent of binary

representation

128 64 32 16 8 4 2 1

Java's 8 Primitive Data Types and the String Class

Data Type Description Examples

byte The byte data type is an 8-bit integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).

-34, 0, 17

short The short data type is a 16-bit integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

-34, 17653, 0

int The int data type is a 32-bit integer. It has a minimum value of - 2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).

-34, 32, -787632,

123443235

long The long data type is a 64-bit signed integer. It has a minimum value of - 9,223,372,036,854,775,808 and a maximum of 9,223,372,036,854,775,807.

-123443235, 0, 1,

381234432327

float The float data type is a single-precision 32-bit IEEE 754 floating point. 1256.7f, 123.4f

double The double data type is a double-precision 64-bit IEEE 754 floating point. -3456.0, 3256.543

boolean The boolean data type has only two possible values true, false

char The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

A char is enclosed in single quotes.

'a', '45'

String The String class represents character strings. All string literals in Java are

enclosed in double quotes, and are implemented as instances of this class.

“Hello”, “hElLo”,

“a”, “45”