CSCE 111 Final
The enhanced for loop can be used to change the elements in an array.
False
The following statement is valid: double xyz = 4,867.50;
False
The new key word is used to define a new type in the program.
False
A single copy of a class's static field is shared by all instances of the class.
True
A value-returning method can return a reference to a non-primitive type.
True
An array can only hold values of a single type.
True
Which of the following is not a valid string in Java?
"A" "\u0048\u0065\u006C\u006F World" "radius = '20\"" "radius = '20'"
What is the output of the following code? int total = 0; for (int a = 0, b = 1; a < 5 || b > 6; a++, b--) { total +=b; } System.out.println(total);
-5
What would be the value of x after the following statements were executed? int x = 10; switch (x) { case 10: x += 15; case 12: x -= 5; case 20: x = 10; break; default: x *+ 3; }
10
How many times will the following for loop be executed? for (int count = 10; count <= 21; count ++) System.out.println("Java is amazing!!!");
12
Consider the following program: for (int loop1 = 0l kiio1 <= 10; loop1++) for (int loop2 = 0; loop2 <= 10; loop2++) System.out.println("1");
121
What will be the value of x[8] after the following code has been executed? final int SUB = 12; int[] x = new int [SUB]; int y = 100; for (int i = 0; i < SUB; i++) { x[i] = y; y += 10; }
180
What will be the output of this code? public class Storage { private int a; private int b; private int c; public Storage (int x) { a = x; b = a + x; c = b + x; } public int getC() { return c; } } public class Test { public static void main(String [] args) { Storage s = new Strong (3); Storage t = new Storage (5); System.out.print(s.getC() + t.getC()); } }
24
What would b the value of bonus after the following statements are executed? int bonus, sales = 500; if (sales > 400) bonus = 100; if (sales > 250) bonus = 50; if (sales > 50) bonus = 25; else bonus = 0;
25
What will be printed after the following code is executed? for (int number = 3; number <= 14; number += 3) System.out.print(number + ", ");
3,6,9,12
The short data type may contain values in the following ranges of values:
32,756 to +32,767
What will be the value of x after the following code is executed? int x = 12; do { x *= 3; } while (x < 5);
36
What will be the output after the following code is executed? int [] a = {5, 4, 2, 5, 7}; for (int v : a) System.out.print(v);
54257
What is the value of ans after the following code has been executed? int a = 27; int b = 10; and = 53; if (a < b); and += b;
63
What is the output of the following code fragment? String x = "Howdy Aggies!"; String y = "Gig em!"; if (y.charAt(2) == x.charAt(7)) System.out.println(y.length()); else System.out.println(x.length());
7
What is the value of a? double a = 25 / 4 + 4 * 10 % 3;
7.0
What does the following code display? int f = 9; double g = 12.1; System.out.printf("%d %f\n", f, g);
9 12.1000
What will be printed when the following code is executed? double x = 98765.456; DecimalFormat formatter = new DecimalFormat("#, ##0.0"); System.out.println(formatter.format(x));
98,765.5
Which of the following statements is FALSE about classes and objects?
A class is an instance of an object
A runtime error is usually the result of:
A logical Error
In the following code, Integer.parseInt(str), is an example of ________. int num; String str = "555"; num = Integer.parseInt(str) + 5;
A value-returning method
RAM is usually:
A volatile type of memory, used only for temporary storage
What will be the results of the following code? final int ARRAY_SIZE = 5; double [] x = new double [ARRAY_SIZE]; for (int i = 1; i < ARRAY_SIZE; i++) { x[i] = 10.0; }
All the values, except the first, are set to 10.0
Each repetition of a loop is known as what?
An iteration
When does array bounds checking occur?
At runtime
The contents of a variable cannot be changed during runtime, only while the code is compiling.
False
A parameter variable's scope is always the class in which the parameter is declared.
False
An advantage of ArrayList over arrays is that they can hold values of different types.
False
An array automatically expands in size to accommodate the items stored in it.
False
An instance of a class must exist in order for values to be stored in a class's static fields.
False
In Java, if x is declared as: final int x = 5; the assignment expression x += 3; will change variable x's value to 3 only if 3 is greater than its current value.
False
In a for statement, the control variable must be incremented by 1 for each iteration.
False
In a while loop, the control variable cannot be tested against a constant value.
False
In an array of objects, each object referenced must have a data field containing its subscript.
False
In nested if statements, if the inner if statements evaluates to false, the outer if is automatically false.
False
In the method header the private method modifier means the method is available to code outside the class.
False
In the method header, the method modifier static means that the method belongs to a specific object, not the class.
False
It doesn't matter where you declare the variables, as long as they are somewhere in the program.
False
JOptionPane uses an instance method to get keyboard input from the user.
False
Java runs differently on different CPU architectures.
False
One class can have multiple constructor methods, but one of them must be a no-arg constructor.
False
Program code can only have one execution path(flow).
False
Suppose we have the following declarations: int i = 4, j = 1; The expression, (i * (j / i)) == 1 , evaluates true or false?
False
The public access specifier for a field indicates that the attribute is accessible to any method, as long as it is the same class.
False
The static method modifier means the method is available only to other private methods in the class.
False
The while loop will always execute once.
False
When an array is given as an argument to a method, the method cannot change the array as it is not in its scope.
False
When the break statement is encountered in a loop, all following statements in the loop body are ignored, and the execution moves to start of the loop body.
False
When the continue statement is encountered in a loop, the execution continues from the next line of code after the body of the loop.
False
Which of the following will open a file name MyFile.txt and allow you to read data from it?
File file = new File ("MyFile.txt"); Scanner inputFile = new Scanner (file);
The JVM periodically performs this process to remove unreferenced objects from memory.
Garbage Collection
The name of a variable is known as its ...
Identifier
How many "Java" are displayed from the following code fragment? int count = 0; while (count < 3) System.out.println("Java"); count ++;
Infinite
_______ is the process of inspecting data given to the program by the user and determining if it is valid.
Input validation
Which one of these is FALSE about the finalize method?
It accepts as an argument as an argument the instance to be destroyed
To alter the size of an array "double [] d = new double [15];" after it has been declared:
It cannot be done
The following statement creates an ArrayList object. What is the purpose of the <File> notation? ArrayList<File> arr = new ArrayList<File> ();
It specifies that only File objects may be stored in the ArrayList object
Each different type of CPU has its own...
Machine Language
A java program must have one of these:
Main method
Breaking a program down into small manageable methods
Makes problems more easily solved, allows for code reuse, simplified programs, makes the code easier to read
A program is a sequence of instructions stored in:
Memory
If numbers is a two-dimensional array, which of the following would give the length of row r?
None of the above: numbers.length number.length[r] numbers[r].length[r] numbers.length(r)
What will be returned from the following method? public static float [] getValue (int x)
None of the above: a float value an integer an array of integers nothing, as it is a static method
The keyword "this":
None of these: refers to the method in which it is called can only be used in a static context cannot be used anywhere other than the class constructor
Which method return type is invalid?
None of these: int, double, void, string...
Values that are sent into a method are called ________.
None of these: modifiers, types, controllers, literals
What will be displayed as a result of executing the following code? public class Test { public static void main(String[] args) { int value1 = 9; System.out.println(value1); int value2 = 45; System.out.println(value2); System.out.println(value3); int value3 = 16; } }
Nothing, this is an error
This type of loop will always be executed at least once.
Post-test loop
What is the output of the following code segment? System.out.print("Print\t" + 3 + "\tthis");
Print 3 this
The term ______ refers to a two dimension array with different sizes for one of the dimensions
Ragged Array
Which of the following values can be passed to a method that has a long parameter variable?
Short and Int (no double)
If you have defined a class SmartPhone with a public static method assignNetwork(), and a client created a SmartPhone object referenced by the variable iphone, which of the following will call the assignNetwork() method from the client class?
SmartPhone.assignNetwork();
When the + operator is used with strings, it is known as the:
String concatenation operator
Variables are:
Symbolic names made up by the programmer that represents location in the computer's RAM
The major components of a typical computer system consists of:
The CPU, Input/Output devices, Main memory, Secondary storage devices
You can use the method to determine whether a file exits
The File class's exists method
Assume the class BankAccount has been created, and the following statement correctly creates an instance of the class: BankAccount account = new BankAccount(1500); What is true about the following statement? System.out.println(account);
The account object's toString method will be implicitly called
The header of a value-returning method must specify this
The arguments required by the method and the data type of the return value
In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number: "); int number = keyboard.nextInt(); while (number < 50 && number > 70) { System.out.print("Enter another number: "); number = keyboard.nextInt(); }
The boolean condition can never be true
when a computer is running a program, the CPU is engaged in a process formally known as:
The fetch/decode/execute cycle
In the following Java statement, what value is stored in the variable "name"? String name = "James Bond";
The memory address where "James Bond" is located
What is the error in the following code? public static int methodA (int x, double y) { double sum = x + y; if (sum > 45) System.out.println ("Sum greater than 45"); return sum; }
The method return type is int, but returns a double
If the following Java statements are executed, what will be displayed? System.out.println("The top three are winners"); System.out.print("George, the Giant\n"); System.out.print("Ben, the Barbarian"); System.out.println("Aaron, the Alligator");
The top three are George, the Giant Ben, the BarbarianAaron, the Alligator
Given the following code, what will be the value of the finalAmount when it is displayed? public class Order { private int orderNum; private double orderAmount; private double orderDiscount; public Order (int orderNumber, double orderAmt, double orderDisc) { public int getOrderAmound() { return orderAmount; } public int getOrderDisc() { return orderDisc; } } public class CustomerOrder { public static void main (String[] args) { int orderNum = 1234; double ordAmount = 580.00; double discountPer = .1; Order order; double finalAmount = order.getOrderAmount() - order.getOrderAmount() * order.getOrderDisc(); System.out.prinln("Final order amount = $" + finalAmount); } }
There is an error because the object order has not been created.
Which of the following is false about static methods?
They are called from an instance of the class
What will be the value of a after the following code is executed? int a = 5, b = 10; while (b < 100) { a += b' }
This is an infinite loop
Which of the following statements is FALSE about static variables, methods and constants?
To declare static variables and methods, use the final static modifier
The purpose of validating the results of the program is:
To make sure the program solves the problem
In the following code, how would you access the YELLOW constant? enum TrafficLight { RED, YELLOW, GREEN }
TrafficLight.YELLOW
A class is not an object, but a description of an object
True
Because the || operator performs short-circuit evaluation, your boolean expression will generally be evaluated faster if the subexpression that is most likely to be true is on the left.
True
Constants, variables, and the values of expressions may be passed as arguments to a method.
True
If a class has a has a method named finalize, it is called automatically before garbage collection.
True
If you write a toString method for a class, Java will automatically call the method any time you concatenate an object of the class with a string.
True
In computers, data stored in binary form is represented as a series of 0's and 1's.
True
Java provides unary operators designed just for incrementing variables.
True
The Java Virtual Machine is responsible for executing Java byte code.
True
The PrintWriter constructor will truncate and overwrite an already existing file.
True
The if/else statement will execute one group of statements if its boolean expression is true or another group if its boolean expression is false.
True
The key word "this" is the name of a reference variable that refers to an instance method's invoking object.
True
The word "import" in Java program is a reserved word.
True
When an object reference is passed to a method, the method may change the values in the object.
True
When two Strings are compared using the "equals" method, their case is important.
True
When you open a file with the Scanner class, the class can potentially throw an IOException.
True
What would be the results of the following code? int [] array1 = new int[25]; ... // Code that will put values in array 1 int value = array1[0]; for (int a = 1; a < array1.length; a++) { if (array1[a] < value) value = array1[a]; }
Value contains the lowest value in array1
In Java, _______ must be declared before they can be used.
Variables
Key words are:
Words that have a special meaning in the programming language, and words that cannot be re-defined by the programmer
What would be the results of the following code? int [] x = { 43, 24, 54, 95, 51, 54, 83, 48, 67}; int a = 8; if (x[2] > x[5]) a = 6; else if (x[2] < x[5]) a = 7;
a = 8
When an array is passed to a method
a reference to the array is passed, it is passed just as an object, the method has direct access to the original array
To access the element at the 1st row and the 5th column of the two dimensional array a, we would use:
a[0][4]
What will be printed after the following code is executed? char [] str = { 'a', 'b', 'c', '4', '5', '6'}; int m = 0; while (m < 6) { if (str [m] != '4' && str [m] != '5') System.out.print(str [m]); m++; }
abc6
When an object, such as a String, is passed as an argument, it is:
actually a reference to the object that is passed
The keyword 'new':
allocates memory for the object
The import statement:
allows programs to access classes that are already defined elsewhere
Values stored in local variables...
are lost between calls to the method in which they are declared
Methods are commonly used to
break a problem down into small manageable pieces
In memory, an array of String objects...
consists of elements. each of which is a reference to a String object
Local variables can be initialized with...
constants, parameter values, and the results of an arithmetic operation.
A loop that repeats a specific number of times is known as a(n)
counter-controlled loop
Which of the following would be a valid method call for the following method? public static void decideWinner (char c, int num1, double num2) { int winner; winner = num1 * (int)num2; System.out.prinln("The winner is " + winner); }
decideWinner ('c', 15, 9);
Given the following statement, which statement will write "CS111" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt");
diskOut.println("CS111");
This type of loop is ideal in situations where the exact number of iterations is known.
for loop
Overloading means multiple methods in the same class...
have the same name, but different parameter lists
If char is a character variable, which of the following if statements is written correctly?
if (char == 'I')
We want to write a program that will "CS111" on 100 different lines. How can we implement this with a while loop?
int a = 1; while (a < 101) { System.out.prinln("CS111"); a++; }
A deep copy of an object:
is an operation that copies an object, and all the objects it references
When an argument is passed to a method...
its value is copied into the method's parameter variable
Local Variables....
lose the values stored in them between calls to the method in which the variable is declared and they are only meant for use in the method they are declared
What will the values of ans, x, and y after the following statements are executed? int ans = 0, x = 15, y = 25; if (x >= y) { ans = x + 10; x -+ y; } else { if (x < y) { ans = y + 1-; } y +=x; }
none of the above
When a field is declared static, there will be...
only one copy of the field in memory
In the Java programming language, an argument is used to ________.
provide values to an invoked method
Which of the following is a correct method header for receiving a two-dimensional array as an argument of size [5,6]?
public static void passArray (int [] [] arr)
Which of the following is a primitive data type?
short, long, float, int, double...
A syntax error differs from a runtime error in that:
syntax errors are reported by the compiler
In order to do a binary search on an array...
the array must first be sorted
The scope of a private instance field is...
the instance methods of the same class
When two variables "String a" and "String b" are compared using "==":
the memory addressed of "a" and "b" are compared to see if they are the same
A parameters variable's scope is
the method in which the parameter is declared
If you attempt to perform an operation with a null reference variable:
the program will terminate with an exception
If an if/else statement, if the boolean expression if false:
the statement or block following the else is executed
If object1 and object2 are objects of the same class, to make object2 a copy of object1:
write a copy method that will make a field by field copy of object1 data members into object2 data members
What will be the value of x and y as a result of the following code? int x = 2, y = 8; x += y--;
x = 10, y = 7
What will be displaced as a result of executing the following code? int x = 8, y = 20; x += 32; if (x > 32) y /= 4; System.out.println("x = " + x + ", y = " + y);
x = 40, y = 5
What would be the results after the following code was executed? int [] x = {23, 55, 83, 19}; int [] y = {36, 78, 12, 24}; { x{a} = y[a]; y[a] = x[a]; }
x [] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24}
For the following code, which statement is not true? public class Sphere { private double radius; public double x; public double y; private double z; }
z is available to code that is written outside the Sphere class