MidTerm - Discover Dalton State

advertisement
Introduction to Programming I
CMPS 1301 (Hawkins)
Exam I: Chapters One to Five
Questions are 3 points each unless otherwise noted.
Name ____________________
True / False:
1.
The statements within a method execute only if and when the method is called.
2.
A variable can hold more than one value at a time
3.
(6 < 10) && ((1 + 2) == 3)
Answer the following:
4.
5 points
Identify at least five errors in the following code:
public class Test {
public static void main(String []
integer num = 4;
double square;
) {
square = squareMethod(num)
System.out.println(num + " squared is " square);
public static int squareMethod( number ) {
double value = number * number;
}
}
5.
Show the output of the following code:
public class Arithmetic {
public static void main(String[] args) {
int x = 3;
int y = 3 + 7 * 4 + 2;
System.out.println(5 > 4);
System.out.println(x / 4);
System.out.println(y);
}
}
1
Answer the following:
6.
Show the output of the following code:
public class Grade {
public static void main(String[] args) {
int score = 45;
char grade;
if score > 50
grade = 'A';
elseif score > 45;
grade = 'B';
elseif score > 40;
grade = 'C';
else
grade = 'D';
System.out.println("Grade is: " + grade);
}
}
7.
Show the output of the following code:
public class Premium {
public static void main(String[] args) {
int trafficTickets = 1;
int age = 20;
char gender = 'F';
int premium = 300;
double extraPremium = 0;
if (trafficTickets > 3 || age < 25)
extraPremium = 200;
if (gender == 'F')
extraPremium = extraPremium + 50;
System.out.println("Policy amount is " +
(premium + extraPremium));
}
}
2
Matching:
8.
Package
9.
Boolean variable
10.
11.
a.
Combination of the method name
and the number, types and order
of arguments
b.
Performs an action based on one
alternative
c.
Holds only one of two values
(true or false)
d.
One step follows another
unconditionally
e.
Group of built-in Java classes
Signature
Sequence Structure
Answer the following:
12.
Write a complete program that calculates the price of a medium pizza based on
the number of toppings desired. A plain cheese pizza costs $5.00 plus 50 cents
for each additional topping.
5 points
3
Answer the following:
13.
What is the output of the following:
public class Test {
public static void main(String[ ] args) {
int num1 = 5;
double num2 = 2.0;
double num3;
Simple(num1);
num3 = Simple(num1, num2);
System.out.println("result is " +num3);
}
public static void Simple(double number) {
System.out.println("number is " +number);
}
public static void Simple(int number) {
System.out.println("We will use " +number);
}
public static double Simple(int a, double b) {
double number;
if (a > b)
number = a – b;
else
number = a + b;
return number;
}
}
14.
What is the output of the following code: (Indent the statement correctly first to
help you read the program)
int grade = 92;
if (grade >= 90)
if (grade > 95)
System.out.println("This is Step1");
else
System.out.println("This is Step2");
else
System.out.println("This is Step3");
4
Multiple Choice:
15.
In Java, this reserved keyword means that a method is accessible and usable
even though no objects of the class exist.
a. class
b. public
c. static
d. void
16.
According to Java naming convention, which of the following is a constant?
a. MAXINTEGER
b. MAX-INTEGER
c. max_integer
d. MAX_INTEGER
17.
Analyze the following two codes where number is defined earlier in the program:
Code 1:
Code 2:
boolean even;
if (number % 2 == 0)
even = true;
else
even = false;
boolean even = (number % 2 == 0);
a.
b.
c.
d.
Code 2 has syntax errors.
Code 1 has syntax errors.
Both Code 1 and Code 2 have syntax errors.
Both Code 1 and Code 2 are correct, but Code 2 is better.
18
Which of the following is a correct call to a method declared as
public static void aMethod(char code)?
a. void aMethod('V');
b. aMethod(char 'V');
c. aMethod('V');
d. answer = aMethod('V');
19.
If you attempt to add a double, an int, and a byte, the result will be:
a. double
b. int
c. byte
d. error message
20.
The expression (int)(Math.random( ) * 10) + 1 returns:
a.
b.
c.
d.
an integer between 1 and 10
an integer between 0 and 10
an integer between 0 and 9
an integer between 1 and 11
5
Multiple Choice:
21.
What is the printout of the following switch statement?
char ch = 'b';
switch (ch) {
case 'a':
case 'b':
case 'c':
case 'd':
}
a.
b.
c.
d.
22.
System.out.print(ch);
System.out.print(ch);
System.out.print(ch);
System.out.print(ch);
bcd
b
bb
bbb
Suppose a Scanner object is created as follows:
Scanner input = new Scanner(System.in);
What method do you use to read a String value?
a. input.nextInput( );
b. input.String( );
c. input.nextString( );
d. input.next( );
23.
24.
Methods that retrieve values are called:
a. static methods
b. accessor methods
c. class methods
d. mutator methods
What is the value of y after the following code fragment is executed:
int x
int y
if (x
y =
a.
b.
c.
d.
= 4;
= 5;
< 3);
1;
y stays at 5
y becomes 1
y becomes 4
the code has syntax error
6
Multiple Choice:
25.
Analyze the following code:
public class Test {
public static void main(String[] args) {
int n = 2;
xMethod(n);
System.out.println("n is " + n);
}
public static void xMethod(int n) {
n = n + 1;
}
}
a.
b.
c.
d.
The code prints n is 1
The code prints n is 2
The code prints n is 3
The code has a syntax error because xMethod does not return a value
BONUS PROBLEM
(4 points)
Write a program that asks the user to enter a number then displays the number along with
a statement determining if the number is even or odd.
Example: if user enters 28, the program should display
28 is an even number
7
Download