Вы находитесь на странице: 1из 6

Programming Concepts COP 2510 Summer 2015

MIDTERM EXAM 1
(Score weight: 10% of total score)
Date: 05/27/2015

NAME: SOLUTION

Time: 3:30 PM ~ 4:45 PM

UID:

Set A

15 x 4 = 60

1) When executing a program, the processor reads each program instruction from
A) secondary memory (storage)
B) the Internet
C) registers stored in the processor
D) main memory
E) could be any of these
Answer: D
Explanation: D) The program is first loaded from secondary memory into main memory before
it is executed so that the processor is not slowed down by reading each instruction.

2) Which memory capacity is the largest?


A) 1,500,000,000,000 bytes
B) 100 gigabytes
C) 3,500,000 kilobytes
D) 10 terabyte
E) 12,000,000 megabytes
Answer: E
3) Which phase of the fetch-decode-execute cycle might use a circuit in the arithmetic-logic
unit?
A) fetch
B) decode
C) execute
D) during fetch or execute, but not decode
E) could be used in fetch, decode or execute phase
Explanation: C) The fetch phase retrieves (fetches) the next program instruction from memory.
The decode phase determines which circuit(s) needs to be used to execute the instruction. The
instruction is executed during the execute phase. If the instruction is either an arithmetic
operation (like add or multiply) or a logical operation (like comparing two values), then it is

carried out by the ALU.

4) Java is an example of a(n)


A) machine language
B) assembly language
C) high-level language
D) fourth generation language
E) both C and D
Explanation: C) While Java was created during the fourth generation, it is clearly a high-level
language. Fourth generation languages are tools wrapped inside of programs so that the user has
the flexibility to write some code to executed from within the program. Example: XBase++,
LINK
5) Comments should
A) rephrase the code it explains in English
B) be insightful and explain what the instruction's intention is
C) only be included in code that is difficult to understand
D) be used to define variables whose names are not easy to understand
E) all of the above
Answer: B
Explanation: B) One might answer E, but that then includes A and C, making "all of the above"
incorrect. Comments should not rephrase in English what an instruction says, but instead should
explain what that instruction is doing in relation to the program. Introductory programmers often
have difficult explaining their code and wind up stating the obvious in their comments. While
answer D is partially correct, it is not entirely true,all variables should have comments that
explain their use.

6) Which character below is not allowed in an identifier?


A) $
B) _
C) 0 (zero)
D) q
E) ^
Answer: E
7) A unique aspect of Java that allows code compiled on one machine to be executed on a
machine of a different hardware platform is Java's
A) bytecodes
B) syntax
C) use of objects
D) use of exception handling

E) all of the above


Answer: A

8) An error in a program that results in the program outputting $100 instead of the correct
answer, $250 is
A) a programmer error
B) a syntax error
C) a run-time error
D) a logical error
E) a snafu
Answer: D
9) Which of the following is a legal Java identifier?
A) 1ForAll
B) oneForAll
C) one/4/all
D) 1_4_all
E) 1forall
Answer: B
10) Mistyping "println" as "printn" will result in
A) a syntax error
B) a run-time error
C) a logical error
D) no error at all
E) converting the statement into a comment
Answer: A
11) Consider the following statement:
System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night");
This statement will output ________ lines of text
A) 1
B) 2
C) 3
D) 4
E) 5
Answer: B
Explanation: B) The \t escape sequence inserts a tab, but leaves the cursor on the same line. The
\n escape sequence causes a new line to be produced so that "4 dinner" is output on the next line.
The escape sequence \r causes the carriage to return (that is, the cursor to be moved back to the
left margin) but because it does not start a new line, "2night" is output over "4 dinn" resulting in
a second line that looks like "2nighter".

12) What value will z have if we execute the following assignment statement?
float z = 5 / 10;
A) z will equal 0.0
B) z will equal 0.5
C) z will equal 5.0
D) z will equal 0.05
E) none of the above, a run-time error arises because z is a float and 5 / 10 is an int
Answer: A
Explanation: A) 5 and 10 are both int values, so 5 / 10 is an integer division. The result is 0.
Even though z is a float and can store the real answer, 0.5, it only gets 0 because of the integer
division. In order to get 0.5, we would have to first cast 5 or 10 as a float.
13) Given the following assignment statement, which of the following answers is true regarding
the order that the operators will be applied based on operator precedence?
a = (b + c) * d / e - f;
A) *, /, +, B) *, +, /, C) +, *, /, D) +, /, *, E) +, -, *, /
Answer: C
Explanation: C) Order of precedence is any operator in ( ) first, followed by * and / in a left-toright manner, followed by + and - in a left-to-right manner. So, + is first since it is in ( ),
followed by * followed by / since * is to the left of /, followed finally by -. Both * and / are at
the same level of precedence but evaluated left-to-right. The same is true of + and -.

14) Assume that x, y, and z are all integers (int) 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
Answer: A
15) What is output with the statement System.out.println(""+x+y); if x and y are int values where
x=10 and y=5?
A) 15
B) 105
C) 10 5
D) x+y
E) An error since neither x nor y is a String
Answer: B

Set B
20 x 1
1. Write a program that will input some number of cents (less than 100) and output the
number of quarters, dimes, nickels and pennies needed to add up to that amount.
Example input and output are shown below:
Input : 99
Output: 3 quarter(s), 2 dime(s), 0 nickel (s), 4 pennie(s)

Input: 34
Output: 1 quarter(s), 0 dime(s), 1 nickel (s), 4 pennie(s)

import java.util.Scanner;
public class Change
{
public static void main(String[ ] args)
{
Scanner scan = Scanner.create(System.in);
System.out.println("Enter the amount of change");
int amount = scan.nextInt( );
System.out.println("The change for " + amount + " cents is: ");
int quarters = amount / 25;
System.out.println(" " + quarters + " quarters");
amount = amount - quarters * 25;
int dimes = amount / 10;
System.out.println(" " + dimes + " dimes");
amount = amount - dimes * 10;
int nickels = amount / 5;
System.out.println(" " + nickels + " nickels");
amount = amount - nickels * 5;
int pennies = amount;
System.out.println(" " + pennies + " pennies");
}
}

Set C

5 x 2 = 10

1. Write an output statement which will output the following characters exactly as shown:
/ ' \" / ' \
System.out.println("/ \ ' \ \ \" / \ ' \ \ ");

2. Write four different program statements that increment the value of an integer variable
total.

total = total + 1;
total++
++total
total += 1

3. Identify and explain two syntax errors from the following program
public class Enigma
{
public static void main(String[ ] args)
{
System.out.println("Input a String");
int x = scan.nextString( );
int x = 5;
}
}
Variable x is redefined
Variable scan is undefined

10

Вам также может понравиться