midtermPreviewWinter2015Solutions

advertisement
CS 110, Programming Fundamentals I
Central Washington University
Instructor: Tatiana Harrison
Midterm Preview
May 2015
Time allotted: 50 minutes
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 given on the extra credit questions, and points will NOT 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:
Signature:
________
I pledge that this submission is solely my work, and that I have neither
given to, nor received help from anyone.
_____________________________
___
Section
Question Type
Question Numbers
Points Possible
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
VI
Points Scored
100
Extra Credit
Total Including Extra Credit
Midterm Preview, Page 1 of 8
CS 110, Programming Fundamentals I, Fall 2013; Central Washington University, Tatiana Harrison
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
Midterm Preview, Page 2 of 8
Syntax errors are mistakes that the programmer has made that violate the rules of
the programming language.
CS 110, Programming Fundamentals I, Fall 2013; Central Washington University, Tatiana Harrison
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.
B.
C.
D.
12.
If x is greater than 0 and x is less than 99
If x is greater than 1 and x is less than 99
If x is (0 or greater) and x is less than 99
If x is (0 or greater) and x is at most 99
What does the following piece of Java code output:
String sentence = “heya”;
System.out.println(sentence.toLowerCase());
A.
B.
C.
D.
heya
Heya
HeYa
HEYA
Midterm Preview, Page 3 of 8
CS 110, Programming Fundamentals I, Fall 2013; Central Washington University, Tatiana Harrison
13.
Major components of a typical modern computer consist of:
A.
B.
C.
D.
14.
The Central Processing Unit
Input/output devices
Secondary storage devices
All of the above
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.
B.
C.
D.
E.
F.
15.
x = 24, y = 7
x = 24, y = 8
x = 25, y = 7
x = 25, y = 8
x = 26, y = 7
x = 26, y = 8
What would be the value of percentVal after the following statements are executed?
A.
B.
C.
D.
E.
0.05
0.04
0.03
0.02
0.01
Midterm Preview, Page 4 of 8
double percentVal = 0.0; int purchase = 950;
char cust = 'N';
if (purchase > 1000){
if (cust == 'Y')
percentVal = .05;
else
percentVal = .04;
} else {
if (cust == 'Y')
percentVal = .03;
else
percentVal = .02;
}
percentVal = .05;
CS 110, Programming Fundamentals I, Fall 2013; Central Washington University, Tatiana Harrison
Section III: Find the Error
16.
This question is worth 20 points; partial credit given
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;
}
Midterm Preview, Page 5 of 8
CS 110, Programming Fundamentals I, Fall 2013; Central Washington University, Tatiana Harrison
Your Answer : ________10___________________________________________________________
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 : _____________City___________________________________________________
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 : ______________14______________________________________________________
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:
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) {
Midterm Preview, Page 6 of 8
CS 110, Programming Fundamentals I, Fall 2013; Central Washington University, Tatiana Harrison
}
}
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, twoor 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.
B.
C.
D.
E.
F.
24.
A single line comment begins with the characters \\
A multiple-line comment begins with */
The following is a syntactically correct comment: // */ */ //////////
Single-line comments are ignored by the compiler
Multiple-line comments are not ignored by the compiler
Multiple-line comments must end with the characters /*
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.
B.
C.
D.
E.
F.
The body of the for-loop will be executed 5 times
The i+=2 part of the for loop will be performed 6 times
The output produced by the for loop will be -113579
The i+=2 part of the for loop is performed as many times as the i<10 check is performed
The i<10 part of the for loop is the initialization portion, and is executed only once
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.
Midterm Preview, Page 7 of 8
CS 110, Programming Fundamentals I, Fall 2013; Central Washington University, Tatiana Harrison
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
27
26
25
24
23
22
21
20
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
'a', '45'
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.
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.
Midterm Preview, Page 8 of 8
“Hello”, “hElLo”,
“a”, “45”
CS 110, Programming Fundamentals I, Fall 2013; Central Washington University, Tatiana Harrison
Download