Exam I (Review List) - Answer Key
Exam I (Review List) - Answer Key
Exam I (Review List) - Answer Key
This list has no intention to be comprehensive and just because you know how to answer all of
its questions it does not mean you can assume you are prepared for exam I.
variables declaration
built-in (primitive) data types: byte, short, int, long, float, double, char, and boolean
01) How should you declare a variable named “c” to store single letters?
a) String c;
b) char c;
c) letter c;
d) text c;
02) How should you declare a variable named “rooms” to store the number of rooms in a house?
a) int rooms;
b) double rooms;
c) float rooms;
d) String rooms;
03) How should you declare a variable named “x” to store the number 0.548?
a) int x;
b) double x;
c) float x;
d) String x;
04) How should you declare a variable named “lampType” to store the text value “led”?
a) int lampType;
b) double lampType;
c) float lampType;
d) String lampType;
int x = 9;
int x = 7;
int x = 9;
x = 7;
int x = 8;
int y = 3;
16) Circle the names that are valid identifiers name (in Java).
17) Circle the names of variables that follow the camel-case convention.
18) If you were to model a computer monitor using features such as screen size and resolution,
also adding the ability to turn it on/off and increase/decrease its brightness , what are
considered attributes of the computer monitor object?
a) screen size and resolution
b) screen size and turn it on/off
c) turn it on/off and increase/decrease its brightness
d) resolution and increase/decrease its brightness
19) If you were to model a power strip, which of the features below would make sense to define
as attributes of this model?
input voltage, color, number of children, student ID, volume, number of outlets, on/off, distance
from earth, number of pages, miles per hour
the String class and basic methods such as format, length, charAt, and substring
String concatenation
20) Which String method is used to determine the number of characters of a String object?
a) size
b) length
c) len
d) numberOfCharacters
21) Which String method is used to retrieve the character at a specified index position?
a) [int index]
b) at(int index)
c) charAt(int index)
d) char(int index)
22) Consider the following:
String s = “Sam”;
String h = “Ham”;
String out = s + “ and ” + h;
standard input using the Scanner class together with the System.in object, including how to use basic
methods such as nextLine, nextInt, and nextDouble
21) How should you initialize a Scanner object to read from the standard input and saving its
reference to a variable named “sc”? sc = new Scanner(System.in);
24) What happens if you use “nextDouble( )” on a Scanner object associated with the standard
input and the user types the number “3.5” using double quotes?
a) nextDouble( ) will automatically convert “3.5” (String) to 3.5 (double)
b) nextDouble( ) will throw an exception and the program will crash
c) nextDouble( ) will ignore the input
d) nextDouble( ) will wait until the user types in a valid double value
25) What’s the different between “print” and “println” methods defined in Java’s OutputStream
class? println adds a new line at the end of the output
double x = 3.123;
int y = 7;
String out = String.format("x = %.2f and y = %d", x, y);
System.out.println(out);
27) Evaluate each of the following boolean expressions. Consider that the following variables
were declared and initialized as shown.
int x = 18;
int y = 7;
int z = 4;
a) (x <= z) || (x % z > -5)
true
b) z % 2 == 0
true
c) x / 3 > z
true
d) (x * 2 == z - 2) && (y % 2 != 0)
false
e) (y > z) && ( (x < z + y) || (x >= 10) )
true
int x = 18;
double y = 7;
int z = 4;
Which of the statements below compile without errors? Assume that each statement is
independent from each other.
x = z; y = x; x = y / 2; z = x; x = (int) y;
an understanding that some methods accept parameters and may also return values;
30) Which of the methods below return values? For the ones that you think return values,
identify the type of value returned.
charAt (from String) length (from String) nextInt (from Scanner)
char int int
int x = 5;
int y = 3;
int z = 2;
if (x > y)
System.out.print("*");
if (y > x / z)
System.out.print("$");
else
System.out.print("@");
int x = 5;
int y = 3;
int z = 2;
if (x > y)
System.out.print("*");
else if (y > x / z)
System.out.print("$");
else
System.out.print("@");
int x = 5;
int y = 3;
int z = 2;
if (x * y < 30) {
System.out.print("*");
System.out.print("$");
if (z - 1 >= x / 5)
System.out.print("@");
}
else
System.out.println("&");
counted loops using the for statement, including nested for loops;
34) Determine the number of iterations of each of the following for loops.
a) exactly 10 times
b) less than 10 times
c) more than 10 times
d) at least 1 time
36) What are two major parts of any object-oriented programming languages?
a) Attributes and Behaviors
b) Behaviors and methods
c) Methods and functions
d) Instance variables and attributes
37) When an object is created, its attributes may need to be initialized in a method. What is
this method called in Java programming language?
a) Methods
b) Constructors
c) Initializer
d) New
a) Objects
b) Classes
c) Formal parameters
d) Actual arguments
a) Overriding
b) Redefinition
c) Overloading
d) Overflowing
44) Given the following code segment, what is in the string reference by s1?
String s1 = "xy";
String s2 = s1;
s1 = s1 + s2 + "z";
a) xyz
b) xy z
c) xy xy z
d) xyxyz
a) 6
b) 9
c) 10
d) 11
46) What is the value of size after the following executes?
String s1 = “Hello world!”;
Int size = s1.length();
a) 11
b) 12
c) 13
d) 14
47) After the following code is executed, which one will evaluate to false?
String s1 = "xyz";
String s2 = s1;
String s3 = s1
s2 = new String(“xyz”);
a) s1.equals (s3)
b) s1 = = s3
c) s2 = = s1
d) s2.equals (s3)
48) What will be printed out after executing the following code?
int x = 2;
int y = 3;
System.out.println(!(x < 3 && y > 2));
a) true
b) false
c) true false
d) false true
a) x >= 3 || y <=2
b) x > 3 && y < 2
c) x >= 3 && y <=2
d) x <=3 || y >= 2
50) What is this one called?
The type of evaluation used for logical and ‘&&’ and logical or ‘||’ expressions. If the
first condition is false in a complex conditional with a logical and the second condition
won’t be evaluated. If the first condition is true is a complex conditional with a logical
or the second condition won’t be evaluated