Programming in Visual Basic



Computer Programming I Instructor: Greg Shaw

COP 2210

Study Guide for Midterm Exam

1. Be able to evaluate arithmetic expressions involving *, /, %, +, -, and parentheses, including integer division and mod operations

2. Be able to translate algebraic expressions and formulas into Java

3. Know how to create objects and call methods that return a value and those that do not

4. Be familiar with Math class methods sqrt, abs, pow, round, min, and max

5. Know the effect of the assignment operator when applied to primitive type variables and to object variables

6. Be able to make decisions using the single-alternative, two-alternative, and multiple-alternative forms of the “if” statement

7. Know the difference between a series of single-alternative “if” statements and a multiple-alternative “if” (aka: “cascaded ifs”)

8. Be able to make decisions used “nested” if statements, the boolean operators, and boolean variables and assignments.

9. Be able to “play computer” or hand trace segments of code containing variable declarations, input, assignment, decisions, and output statements

10. Be able to explain the difference between Java “Documentation Comments” (aka: “Javadoc” comments) and “internal” documentation

***** Sample Questions *****

1. Suppose that num contains a positive 4-digit int such as 1234. Which of the following expressions evaluates to the middle two digits?

a. num / 10 % 10

b. num / 100 % 100

c. num / 10 % 100

d. num % 1000 / 10

e. both c. and d. are correct

2. Suppose int variable grade contains 77. What gets stored in String variable letter?

if (grade >= 90)

letter = “A” ;

else if (grade >= 80)

letter = “B” ;

else if (grade >= 70)

letter = “C” ;

else if (grade >= 60)

letter = “D” ;

else

letter = “F” ;

a. A

b. B

c. C

d. D

e. CDF

3. Suppose int variable grade contains 77. What gets stored in String variable letter?

String letter = "" ;

if (grade >= 90)

letter = “A”

if (grade >= 80)

letter = “B”

if (grade >= 70)

letter = “C”

if (grade >= 60)

letter = “D”

else

letter = “F”

System. out.println( letter ) ;

a. A

b. B

c. C

d. D

e. CD

4. Suppose int variable grade contains 77. What gets stored in String variable letter?

if (grade >= 60)

letter = “D” ;

else if (grade >= 70)

letter = “C” ;

else if (grade >= 80)

letter = “B” ;

else if (grade >= 90)

letter = “A” ;

else

letter = “F” ;

a. A

b. B

c. C

d. D

e. F

5. Which of the following tests is NOT equivalent to:

if (awesome == true)

a. if (awesome)

b. if ( ! (awesome == false) )

c. if ( !awesome == false)

d. all are equivalent

e. none are equivalent

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

int num = 100 ;

if (num < 100)

{

if (num < 50)

{

num = num – 5 ;

}

else

{

num = num – 10 ;

}

}

else

{

if (num > 150)

{

num = num + 5 ;

}

else

{

num = num + 10 ;

}

}

System.out.println(num) ;

a. 95 b. 100 c. 105 d. 110 e. none of the above

======================================================================

Answers:

1. e

Why? WORK IT OUT!

a. 1234 / 10 = 123, 123 % 10 = 3

b. 1234 / 100 = 12, 12 % 100 = 12

c. 1234 / 10 = 123, 123 % 100 = 23

d. 1234 % 1000 = 234, 234 / 10 = 23

2. c

“Cascaded ifs” aka “Multiple-alternative decisions.” Java finds the first true condition and executes the associated statement(s). All other conditions and statements are skipped.

3. d

There are 3 separate single-alternative if statements followed by one 2-alternative if statement. All 4 are executed. The first 2 conditions are false. The third, “grade >= 70” is true, so letter gets “C.” But the next condition, “grade >= 60” is also true, so the “C” gets replaced by a “D.”

4. d

“Cascaded ifs” aka “Multiple-alternative decisions.” Java finds the first true condition and executes the associated statement(s). All other conditions and statements are skipped. The first condition, “grade >= 60”, is true, so letter gets “D.” Because the conditions are not mutually-exclusive (i.e. more than one can be true) the order in which they are tested is crucial. Unlike question 1, here the conditions are tested in the wrong order.

5. d.

All of the expressions are equivalent. Test each one when awesome is true and again when awesome is false and you will see that they all produce the same result.

6. d.

Nested ifs!

• num is initialized to 100.

• The condition “num < 100” is false so the if branch is skipped and the else branch is done.

• The condition “num > 150” is false so the else branch is done and 110 is assigned to num.

Here’s a related question for even more fun: Can you think of different initial values for num that will result in all 4 possible branches being executed?

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download