Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Exam I (Review List) - Answer Key

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

CS 1050 - Computer Science I (Fall 2020)

Exam I - Review List

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;

05) Consider the following:

int x = 9;
int x = 7;

What do you think is going to happen if the code above is evaluated?


a) x is going to be assigned 9 and then 7
b) x is going to be assigned 9 and the following line will be ignored
c) the program won’t compile because x is being redeclared
d) the IDE will send a text message to CIA and you might get arrested
06) Consider the following:

int x = 9;
x = 7;

What do you think is going to happen if the code above is evaluated?


a) x is going to be assigned 9 and then 7
b) x is going to be assigned 9 and the following line will be ignored
c) the program won’t compile because x is being redeclared
d) the IDE will send a text message to CIA and you might get arrested

 arithmetic operators (including the modulus operator and how to perform integer/floating-point division)


 evaluation of arithmetic expressions
 operators precedence and using parentheses to change precedence

07) What’s the result of the expression “9 / 5 + 32”?


33

08) What’s the result of the expression “9 / 5. + 32”?


33.8

09) What’s the result of the expression “9 % 5 + 32”?


36

To answer questions 10-12, consider:

int x = 8;
int y = 3;

10) What’s the result of the expression “(x + 1) / y – 2"?


1

11) What’s the result of the expression “x + 1 / (y – 1)"?


8

12) What’s the result of the expression “x + (1 / y – 2)"?


6

To answer questions 13-15, consider:


double x = 9;
int y = 4;

13) What’s the result of the expression “(x + 1) / y – 2"?


0.5

14) What’s the result of the expression “x + 1 / (y – 1)"?


9.0
15) What’s the result of the expression “x + (1 / y – 2)"?
7.0
 rules for naming variables

16) Circle the names that are valid identifiers name (in Java).

andromeda m&ssi&r $auron 1000stars _sculptor_


stop(please) no_more PLUTO veeeeenuS 7aces
SevenAcCCes univErse station Lun@ mOOn_

 camel-case naming convention for identifiers (variables and class names)

17) Circle the names of variables that follow the camel-case convention.

clockHours MAIN_SPRING wheel mainGear minutes


tooLate Clock HistoricalClock numberOfyears sevenPastNine
goodShape FilledCircle Great Planet distantPlanet

 identification of attributes and behaviors given a model of an object

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;

What’s the value of variable “out”? “Sam and Ham”


What’s the result of “out.substring(8)”? “Ham”

 standard input using the Scanner class together with the System.in object, including how to use basic
methods such as nextLine, nextInt, and nextDouble

23) Which Scanner method is used to read an integer number?


a) next( )
b) next_int( )
c) nextLine( )
d) nextInt( )

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

 standard output using the System.out object

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

26) Consider the following:

double x = 3.123;
int y = 7;
String out = String.format("x = %.2f and y = %d", x, y);
System.out.println(out);

What’s the output?


a) x = 3.12 and y = 7
b) x = 3.123 and y = 7
c) x = 3.12 and y = 7.0
d) x = 3.123 and y = 7.0
 relational operators and how to read (and evaluate) basic boolean expressions
 logical operators (and, or, and not)
 evaluation of more complex boolean expressions with relational and logical operators
 simplification and rules of equivalency of boolean expressions

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

28) Simplify the following boolean expressions.

(a > 5) || ((a > 5) && (b < 8))


a>5
(b < 8) && (b < 8)
b<8
! ( (a > 5) && ( (b > 8) || (b <= 8) ) )
a <= 5
You might want to review “De Morgan’s law”

 type casting of variables of primitive types 

29) Consider the following:

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

println (from OutputStream) format (from String) sqrt (from Math)


String double

 conditional statements using if-else, including nested if constructs

31) What’s the output of the following program? *$

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("@");

32) What’s the output of the following program? *

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("@");

33) What’s the output of the following program? *$@

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.

for (int i = -5; i < 10; i++)


System.out.print(".");
15
for (int i = -5; i <= 10; i++)
System.out.print(".");
16
for (int i = -5; i < 10; i += 2)
System.out.print(".");
8
for (int i = -5; i <= 10; i += 2)
System.out.print(".");
8
for (int i = 0; i < 7; i++)
for (int j = 0; j < 4; j++)
System.out.print(".");
28
for (int i = 0; i < 7; i += 3)
for (int j = 0; j < 4; j++)
System.out.print(".");
12
for (int i = 0; i < 7; i += 3)
for (int j = 0; j < 4; j += 2)
System.out.print(".");
6
for (int i = 0; i < 7; i++) => 7
for (int j = 0; j < i; j++)
System.out.print(".");
0+1+2+3+4+5+6=21
 uncounted loops using the while statement. 

35) How many times will the following loop run?

boolean done = false;


Scanner sc = new Scanner(System.in);
while (!done) {
System.out.print("? ");
done = sc.nextBoolean();
}

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

38) Which of these are overloading constructor?


a) When a constructor takes two parameters
b) When a constructor takes more than one parameter
c) When one constructor is defined in a class.
d) When more than one constructor is defined in a class

39) Which one is the default constructor of class HybridVehicle class?


a) HybridVehicle()
b) HybridVehicleConstructor()
c) HybridVehicle(aModel, aMileage)
d) DefaultConstructor()

40) What are firstName and lastName in

Student aStudent = new Student(firstName, lastName);

a) Objects
b) Classes
c) Formal parameters
d) Actual arguments

41) When do you get a NullPointerException ?

a) When you assign a null value to an object


b) When you try to call a method on an object variable whose value is null
c) When you assign a valid to an object variable whose current value is null
d) When computer runs out of memory space
42) When there are multiple methods with the same name but a different method signature is
called what?

a) Overriding
b) Redefinition
c) Overloading
d) Overflowing

43) What’s the difference between an expression and a statement?


a) An expression is used with numeric data types only whereas a statement can be
used with any data types
b) An expression must have a variable in it but a statement doesn’t need to
c) Expressions return a value after evaluating them but statements don’t return
d) Expressions run faster than statements.

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

45) What’s the value not generated by executing following statement?

int rnd3 = (int)(Math.random()*6) + 5;

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

49) Which expression is equivalent with the following expression?

!(x < 3 && y > 2)

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

e) Logical operator precedence


f) De Morgan’s law
g) Duality principle
h) Short circuit evaluation

You might also like