CSCI 1010 Midterm Review
Which of the following best describes how a while loop works? The loop body is repeated until the controlling boolean expression is true. The loop body is repeated until the controlling boolean expression is false. The loop body is executed once if the boolean condition is true. The loop body is executed once if the boolean condition is false.
The loop body is repeated until the controlling boolean expression is false.
Which of the following keywords is not required when declaring a named constant? final void public static
Void
When is it a good idea to use a count-controlled loop? When the user determines when the loop should stop. When the number of times the loop should repeat is known ahead of time. When the number of times the loop should repeat is unknown. When the value of a boolean variable determines whether or not the loop should end.
When the number of times the loop should repeat is known ahead of time.
What does the following code display? for (int i = 1; i <= 5; i++){if (i == 4)continue;System.out.println("boing");} boing boing boing boing boing boing boing boing boing boing boing boing boing boing
boing boing boing boing
What does the following code display? for (int i = 1; i <= 5; i++) { if (i == 4) break; System.out.println("boing"); } boing boing boing boing boing boing boing boing boing boing boing boing boing boing
boing boing boing
Assuming you have the following declaration: Scanner keyboard; Which is the correct way to read a boolean value? boolean flag = keyboard.next(); boolean flag = keyboard.nextInt(); boolean flag = keyboard.nextBoolean(); boolean flag = keyboard.nextDouble();
boolean flag = keyboard.nextBoolean();
Given the following for loop: for(count = 0; count < 4; count++) System.out.println(count); Which of the following is an equivalent while loop? count = 0 while (count < 4) { System.out.println(count); count++; } count++; while (count < 4) { count = 0 System.out.println(count); } while (count < 4) { count = 0 System.out.println(count); count++; } count = 0 while (count < 4) { count++; System.out.println(count); }
count = 0 while (count < 4) { System.out.println(count); count++; }
Which of the following would be the best name for a variable representing the distance between two points? x distanceBetweenPoints DISTANCE_BETWEEN_POINTS DistanceBetweenPoints
distanceBetweenPoints
A class called Printer has a method called printDocument. This calls a void method in the Printer class called printPage that takes an integer representing the page number as an argument. How should printDocument call printPage with an argument of 4? printPage(4); Printer.printPage(4); Printer printer; printer.printPage(4); Printer printer.printPage(4
printPage(4);
Which of the following is a correct header for a constructor for a class called Employee? public Employee(String name) private Employee(String name) public Employee setEmployee(String name) public void Employee(String name)
public Employee(String name)
How Do you write a class and main block?
public class Name{ public static void main(String[] args)
Given the following method header: public double computeArea(double length, double width) Which of the following would be a legal overloaded method in the same class? public double computeArea(double length) public double computeArea(double side1, double side2) public int computeArea(double length, double width) private double computeArea(double length, double width)
public double computeArea(double length)
Given the following Java code: if (number <= 8)System.out.println("rutabaga"); if (number >= 5)System.out.println("kumquat"); What is displayed if the variable number has a value of 6? rutabaga kumquat rutabagakumquat Nothing is displayed
rutabaga kumquat
Which of the following is the correct way to test if two strings s1 and s2 are stored in the same memory location? s1 = s2 s1 == s2 s1.equals(s2) s1.equalsIgnoreCase(s2)
s1 == s2
Which of the following is the correct way to test if two strings s1 and s2 are exactly the same? s1 = s2 s1 == s2 s1.equals(s2) s1.equalsIgnoreCase(s2)
s1.equals(s2)
Given the following Java code: if (number < 16)if (number > 12)System.out.println("cabbage");elseSystem.out.println("lettuce");elseSystem.out.println("spinach"); What is displayed to the screen if the value of number is 24? cabbage lettuce spinach Nothing is displayed
spinach
A variable can be declared in the initialization part of a for statement. True False
true
Given the following Java code: boolean flag = number < 12 if (!flag) System.out.println("walrus") else System.out.println("narwhal") What is printed to the screen if the value of number is 18? walrus narwhal walrus narwhal Nothing is displayed.
walrus
Given the following delcarations: Scanner keyboard = new Scanner(System.in);String word; Which of the following reads a single word into the variable word? word = keyboard.nextInt(); word = keyboard.nextDouble(); word = keyboard.next(); word = keyboard.nextLine();
word = keyboard.next();
If the body of a while loop needs to contain multiple statements, those statements must be surrounded by which of the following symbols? ( ) [ ] { } < >
{ }
What is the correct fully parenthesized version of the following Java expression? 25 + 3 * 2 < 50 || 88 == 3 - 2 && 21 < 99 (25 + 3) * (((2 < (50 || 88)) == 3) - ((2 && 21) < 99)) ((((((((25 + 3) * 2) < 50) || 88) == 3) - 2) && 21) < 99) ((25 + (3 * 2)) < 50) || ((88 == (3 - 2)) && (21 < 99)) (((25 + (3 * 2)) < 50) || (88 == (3 - 2))) && (21 < 99)
((25 + (3 * 2)) < 50) || ((88 == (3 - 2)) && (21 < 99))
Which of the following is a correct multi-line comment? // This is the first line This is the second line /*This is the first line This is the second line/* */This is the first line This is the second line*/ /*This is the first line This is the second line*/
/*This is the first line This is the second line*/
Consider the following loop that adds up a sequence of numbers: for(int count = 1; count <= n; count++) { next = keyboard.nextInt(); sum = sum + next; } What should the initial value of sum be? 0 1 2 3
0
What does the following code display to the screen? int number = 0; while (number < 5) { System.out.println(number); number++; } 12345 01234 1 2 3 4 5 0 1 2 3 4
0 1 2 3 4
What does the following code display to the screen? int number = 10; do { System.out.println(number); number--; }while (number < 5); 10 9 8 7 6 10 9 8 7 6 5 10 Nothing is displayed.
10
Given the following code: String greeting = "Hello there!";System.out.println(greeting.length()); What is displayed to the screen? 10 11 12 13
12
What is the value assigned to the variable number by the following statement? number = (4 < 3) ? 24 : 13; 4 3 24 13
13
After executing the following statements: int number = 7;number *= 3; What is the value of the variable number? 3 7 10 21
21
What does the expression 14 % 5 evaluate to? 70 2.8 2 4
4
Given the following class definition: Given the following class definition: public class IntTester { void addOneTo(int value) { value = value + 1; } } What is the output of the following statements? IntTester it = new IntTester(); int value = 42; it.addOneto(value); System.out.println(value); 41 42 43 44
42
public class IntWrapper { private int value; public void setValue(int newValue) { value = newValue; } public int getValue() { return value; } } public class IntWrapperTester { public void increment(IntWrapper iw) { int value = iw.getValue(); iw.setValue(value + 1); } } What would the output be for the following statements? IntWrapper iw = new IntWrapper(); IntWrapperTester iwTester = new IntWrapperTester(); iw.setValue(42); iwTester.increment(iw); System.out.println(iw.getValue()); 41 42 43 44
43
Given the following statements: double number1 = 5.7; int number2 = (int)number1; What is the value assigned to the variable number2? 5 5.7 6 7
5
Given the following code: String line = "I love lamp.";System.out.println(line.indexOf("lamp")); What is displayed to the screen? 6 7 8 9
7
How many bits make up a byte?
8 bits
Which of the following conditional operators should you avoid using with floating point data types? < > >= ==
==
What is the relationship between a class and an object? A.A class is a blueprint for ccreating objects B.an object is a blueprint for creating classes C.class defines a behavior for an object D. An object defines a behavior for a class
A class is a blueprint for creating objects
What is a wrapper class? A class that provides methods for manipulating a primitive type. A class that only has a default constructor. A class that only has static methods. A class that only has nonstatic methods.
A class that provides methods for manipulating a primitive type.
What is decomposition? A form of testing that starts by testing a method before testing any other method that calls it. A design strategy that breaks a task down into several subtasks. The use of classes, inheritance, and polymorphism to make a program more modular. The combining of code from several smaller methods into one larger method.
A design strategy that breaks a task down into several subtasks
What is a driver program? A program that has no main method. A program that does not read any input from the user. A program that does nothing but test a specific method. A program that does not generate any output.
A program that does nothing but test a specific method
What is the difference between a public and private instance variable? A public instance variable is declared outside of a method. A private instance variable is declared inside of a method. A public instance variable can be passed to a method. A public instance variable can not be passed to a method. A public instance variable is accessible outside of its class definition. A private instance variable is only accessible inside its class definition . A public instance variable can be modified. A private instance variable can not be modified.
A public instance variable is accessible outside of its class definition. A private instance variable is only accessible inside its class definition
What is a default constructor? A public method that uses one or more arguments to set the values one or more instance variables. A public method that returns the value of an instance variable. A public method that takes no arguments and initializes instance variables. A private method that is used by other methods in the class to perform a specialized task.
A public method that takes no arguments and initializes instance variables
What is an Algorithm?
A set of directions for solving a problem
Which of the following is not allowed in Java? A static method calling another static method of the same class. A static method calling a nonstatic method of the same class without an object to call it on. A nonstatic method calling another nonstatic method of the same class without an object to call it on. A nonstatic method calling a static method of the same class.
A static method calling a nonstatic method of the same class without an object to call it on.
What is the difference between a static variable and an instance variable? A static variable can not be modified after it is initialized. An instance variable can be modified. A static variable can be modified after it is initialized. An instance variable can not. Every object has its own version of a static variable. An instance variable is shared by all objects of a class. A static variable is shared by all objects of a class. Every object has its own version of an instance variable.
A static variable is shared by all objects of a class. Every object has its own version of an instance variable.
What is the difference between the arguments (or actual parameters) and parameters (or formal parameters)? Arguments are the values that are passed to a method. Parameters are variables that store those values. Arguments are variables declared inside of a method. Parameters are variables defined outside of a method. Arguments represent the data items of an object. Parameters describe the operations performed on that object. Arguments are values associated with a specific object. Parameters are values that are not associated with a specific object
Arguments are the values that are passed to a method. Parameters are variables that store those values.
Given the following Java code: if (number < 10)System.out.println("banana"); elseSystem.out.println("artichoke"); What is displayed if the variable number has a value of 12? banana artichoke bananaartichoke artichokebanan
Artichoke
Which of the following is a correct class definition for a class named Monkey, with a public instance variable called business and a public void method called seeNoEvil? public class Monkey; public String business; public void seeNoEvil() { System.out.println("Nothing to see here!"); } public class Monkey { public String business; public void seeNoEvil() { System.out.println("Nothing to see here!"); } } public void seeNoEvil() { public class Monkey; public String business; System.out.println("Nothing to see here!"); } public void seeNoEvil() { public class Monkey { public String business; System.out.println("Nothing to see here!"); } }
B
Which Component of a computer is used to hold a running program and its data? A. The Central Processing Unit B. The Main Memory C. Auxiliary Memory D. Input Output Devices
B. The Main Memory
Which of the following is a program that translates high-level language into low level language? A. the operating system B. A complier C. an interpreter d. a virtual machine
B. a compiler
What output is generated by the following code? int value = 2; switch(value) { case 1: System.out.println("Kamchatka"); break; case 2: System.out.println("Yakutsk"); case 3: System.out.println("Irkutsk"); break; default: System.out.println("Siberia"); } Kamchatka Yakutsk Irkutsk Siberia Yakutsk Yakutsk Irkutsk Yakutsk Irkutsk Siberia
Yakutsk Irkutsk
Given the following code: System.out.println("You had me at \"hello\"."); What is displayed to the screen? "You had me at \"hello\"." You had me at \"hello\". You had me at "hello". "You had me at "hello"."
You had me at "hello".
Which of the following kinds of loops is most useful for repeating an action for each value in an enumeration. A while loop. A do-while loop. A for loop. A for-each loop.
a for-each loop
What is inheritance?
a way of organizing classes by identifying common attributes
System.out.print("One");System.out.println("Two");System.out.print("Three");System.out.println("Four"); What is displayed to the screen? One Two Three Four One Two Three Four OneTwo ThreeFour OneTwo ThreeFour
OneTwo ThreeFour
Given a class called Rectangle with a constructor that takes two doubles as arguments representing the dimensions, which of the following is the correct way to invoke that constructor? Rectangle rect = Rectangle(8.2, 3.5); Rectangle rect = new Rectangle(8.2, 3.5); Rectangle rect; Rectangle(rect, 8.2, 3.5); Rectangle rect; new Rectangle(rect, 8.2, 3.5);
Rectangle rect = new Rectangle(8.2,3.5);
Which of the following kinds of errors is a grammatical mistake in a program? A. Syntax Error B. Run-time error C. Logic Error D. None of the above
Syntax error
Which of the following objects is used to display normal output to the screen? System.in System.out System.err None of the above.
System.out
Which of the following is used to display formatted output? System.out.print System.out.println System.out.printf System.out.printout
System.out.printf
A variable declared in one method can have the same name as a variable declared in another method. True False
TRUE
If no constructors are defined for a class, Java will automatically define a default constructor. True False
TRUE
Method definitions can be marked as private. True False
TRUE
What is unit testing? Running a program to make sure it provides enough information for the user to understand what is going on. Testing the correctness of individual units of code. Fixing syntax errors so that the program compiles. Making sure that a program is indented correctly and contains comments explaining what it does.
Testing the correctness of individual units of code
Why is it a good idea to use a DEBUG flag when tracing variables? The DEBUG flag will end the program and display an error message. The DEBUG flag prevents the program from changing the value of a variable. The DEBUG flag allows the programmer to easily turn the tracing messages on and off. The DEBUG flag can be used to exit a loop.
The DEBUG flag allows the programmer to easily turn the tracing messages on and off.
What does the keyword this refer to? The current method being called. The last variable declared. A brand new object. The object the current method is being called on.
The Object the current method is being called on
What does class interface consist of? The public and private instance variables in the class. The headings of the public and private methods in the class. The public named constants and headings of public methods of the class. The private named constants and headings of private methods of the class.
The Public named constants and headings of public methods of the class
An import statement is required to use the java Math class. True False
FALSE
Every wrapper class has a default constructor. True False
FALSE
Only one constructor may be defined per class. True False
FALSE
Regular methods may be overloaded, but a constructor may not be overloaded. True False
FALSE
A variable declared inside of a block can not be accessed outside of that block. True False
FALSE +
A boolean variable can not be used the controlling boolean expression in a loop. True False
False
which of the following best describes the relationship between classes and objects? A class is an instance of an object. An object is an instance of a class. A class is a method of an object. An object is a method of a class
An object is an instance of a class
What is the difference between a while loop and a do-while loop? A while loop evaluates its controlling boolean expression before the body is executed. A do-while loop evaluates its controlling boolean expression after the loop is executed. A while loop evaluates its controlling boolean expression after the body is executed. A do-while loop evaluates its controlling boolean expression before the loop is executed. A while loop continues as long as the controlling boolean expression is true. A do-while loop continues as long as the controlling boolean expression is false. A while loop continues as long as the controlling boolean expression is false. A do-while loop continues as long as the controlling boolean expression is true.
A while loop evaluates its controlling boolean expression before the body is executed. A do-while loop evaluates its controlling boolean expression after the loop is executed.
Given the following java code, what is displayed to the screen? char letter = 'a'; switch(letter) { case 'a': case 'b': System.out.println("Alaska"); break; case 'c': case 'd': System.out.println("Northwest Territory"); break; default: System.out.println("Greenland"); } Alaska Northwest Territory Greenland Nothing is displayed.
Alaska
All methods must return a value. True False
FALSE
Which is the correct way to define a class called LightBulb with an instance variable called wattage, with an accessor method and a mutator method for that instance variable? public class LightBulb { public int wattage; public int getWattage() { return wattage; } public void setWattage(int newWattage) { wattage = newWattage; } } public class LightBulb { private int wattage; private int getWattage() { return wattage; } private void setWattage(int newWattage) { wattage = newWattage; } } public class LightBulb { public int wattage; private int getWattage() { return wattage; } private void setWattage(int newWattage) { wattage = newWattage; } } public class LightBulb { private int wattage; public int getWattage() { return wattage; } public void setWattage(int newWattage) { wattage = newWattage; } }
D. public class LightBulb { private int wattage; public int getWattage() { return wattage; } public void setWattage(int newWattage) { wattage = newWattage; } }
Which of the following is not a good guideline for a well encapsulated class? Make helping methods private. Place a comment before each public method heading that fully specifies how to use the method. Declare all the instance variables as public. Provide public accessor methods to retrieve the data in an object.
Declare all the instance variables as public
When does the == operator return true when comparing two class variables? If the two class variables refer to two different objects that have the same contents. If the two class variables refer to the same object. All of the above. None of the above.
If the two class variables refer to the same object
When does the equals method return true when comparing two class variables? If the two class variables refer to two different objects that have the same contents. If the two class variables refer to the same object. All of the above. None of the above.
If the two class variables refer to two different objects that have the same contents
public Rectangle(int width, double length)public Rectangle(double width, int length) which constructor will the following statement call? Rectangle rect = new Rectangle(4, 6) The first constructor. The second constructor. Both constructors. Java will generate an error.
Java will generate an error
How are variables of a primitive type different from variables of a class type? The memory location of a variable of a primitive type contains a data value itself. The memory location of a variable of a class type contains a reference to an object. A variable of a primitive type can be passed to a method as an argument. A variable of a class type can not be passed to a method as an argument. A variable of a primitive type can be used as an instance variable in a class. A variable of a class type can not be used as an instance variable in a class. A variable of a primitive type is initialized using the new keyword. A variable of a class type is not.
The memory location of a variable of a primitive type contains a data value itself. The memory location of a variable of a class type contains a reference to an object.
Why does the following code cause a compiler error? public int min(int num1, int num2) { if (num1 < num2) return num1 else if (num2 < num1) return num2 } There are multiple return statements. The < operator can not be used on ints. The values returned are not compatible with the return type. The method doesn't return a value in some cases.
The method doesn't return a value in some cases
Which of the following is not part of the signature of a method? The name of the method. The number of parameters for a method. The types of the parameters of a method. The return type of a method.
The return type of a method
A loop might terminate for some input data values, but repeat infinitely for other values. True False
True
A stub is a simplified version of a method that is sufficient for testing purposes. True False
True
Calling a method on a variable with a value of null will result in an error message. True False
True
Two methods may have the same name if they have a different number of arguments. True False
True
Consider the following code: Employee emp1 = new Employee();emp1.setName("Anna Karenina");Employee emp2 = new Employee();emp2.setName("David Copperfield");emp2 = emp1; Which of the following best describes the contents of the two variables emp1 and emp2 after this code has executed? emp1 refers to the object with the name David Copperfield. emp2 refers to a different object with the same data. emp1 refers to the object with the name David Copperfield. emp2 refers to the same object. emp1 refers to the object with the name Anna Karenina. emp2 refers to a different object with the same data. emp1 refers to the object with the name Anna Karenina. emp2 refers to the same object.
emp1 refers to the object with the name Anna Karenina. emp2 refers to the same object.
The body of a loop can not contain another loop statement. True False
false
Given the following statements: String total = "five" + "seven";System.out.println(total); What is displayed to the screen? total twelve five seven fiveseven
fiveseven
Given the following Java code: if (!(number < 16)) System.out.println("watermelon") else System.out.println("honeydew") What is displayed if the value of number is 13? watermelon honeydew watermelonhoneydew Nothing is displayed.
honeydew
Which of the following examples does not result in an infinite loop? int count = 0; while (count < 5) System.out.println(count); int count = 0; while (count < 5) System.out.println(count); count++; int count = 0; while (count < 5); { System.out.println(count); count++; } int count = 0; while (count < 5) { System.out.println(count); count++; }
int count = 0; while (count < 5) { System.out.println(count); count++; }
Assume there is a class called Leopard with a member function called getNumberOfSpots that takes no arguments and returns an integer value. What would be the correct way to call that method on a Leopard object called laura? int numSpots; laura.getNumberOfSpots(numSpots); laura.getNumberOfSpots(); int numSpots = laura.getNumberOfSpots(); getNumberOfSpots(laura);
int numSpots = laura.getNumberOfSpots();
Which of the following assignments is not legal in Java? int number = 42; double number = 42; int number = 3.14; double number = 3.14;
int number = 3.14;
Given the following Java code: if (number < 16) if (number)>12)System.out.println("cabbage");elseSystem.out.println("lettuce");elseSystem.out.println("spinach"); What is displayed to the screen if the value of number is 10? cabbage lettuce spinach Nothing is displayed
lettuce
A variable that is declared inside of a method is called a _______ variable. instance static global local
local
Which of the following is a legal identifier? my variable my-variable myVariable myVariable!
myVariable
Which of the following boolean expressions evaluates to true if num1 is not equal to num2? num1 != num2 num1 <= num2 num1 >= num2 num1 == num2
num1 != num2
Which of the following statements does not increase the value of the variable number by 1? number += 1; number + 1; number = number + 1; number++;
number + 1;
Which of the following is the correct way to set the value of the variable number to 12? number(12); 12 = number; number = 12; set number to 12;
number = 12;
