Cisc 115 intro to java final exam
In a @return tag statement the description:
describes the return value
Given the following statement, which statement will write "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt");
diskOut.println("Calvin");
This type of loop is ideal in situations where you always want the loop to iterate at least once
do-while loop
Variables of the boolean data type are useful for:
evaluating true/false conditions
A Java program will not compile unless it contains the correct line numbers.
false
All Java statements end with semicolons.
false
An important style rule you should follow when writing if statements is to line up the conditionally executed statement with the if statement
false
Assuming that pay has been declared a double, the following statement is valid. pay = 2,583.44;
false
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.
false
Class names and key words are examples of variables.
false
Colons are used to indicate the end of a Java statement.
false
In Java the variable named total is the same as the variable named Total.
false
In a for statement, the control variable can only be incremented
false
In the for loop, the control variable cannot be initialized to a constant value and tested against a constant value.
false
In the method header the static method modifier means the method is available to code outside the class. True
false
In the method header, the method modifier public means that the method belongs to the class, not a specific object.
false
Java is a case-insensitive language.
false
Java source files end with the .class extension.
false
Only constants and variables may be passed as arguments to methods.
false
Programs never need more than one path of execution.
false
The do-while loop is a pre-test loop.
false
When testing for character values, the switch statement does not test for the case of the character.
false
When the break statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.
false
When two Strings are compared using the compareTo method, the cases of the two strings are not considered.
false
Which of the following is valid?
float w; w = 1.0f;
This type of loop is ideal in situations where the exact number of iterations is known.
for loop
________ refers to the physical components that a computer is made of.
hardware
The ________ statement is used to make simple decisions in Java.
if
Which of the following correctly tests the char variable chr to determine whether it is NOT equal to the character B?
if (chr != 'B')
If chr is a character variable, which of the following if statements is written correctly?
if (chr == 'a')
When using the PrintWriter class, which of the following import statements would you write near the top of your program?
import java.io.*;
If a loop does not contain within itself a way to terminate, it is called a(n):
infinite loop
In Java, you do not use the new operator when you use a(n):
initialization list
Which of the following is a valid declaration for a ragged array?
int[][] ragged = new int[5][];
A sentinel value ________ and signals that there are no more values to be entered.
is a special value that cannot be mistaken as a member of the list
A search algorithm:
is a way to locate a specific item in a larger collection of data
When an argument is passed to a method:
its value is copied into the method's parameter variable
The ArrayList class is in this package.
java.util
Which of the following will compile a program called ReadIt?
javac ReadIt.java
You should always document a method by writing comments that appear:
just before the method's definition
Each array in Java has a public field named ________ that contains the number of elements in the array.
length
To return an array of long values from a method, use this as the return type for the method.
long[]
This part of a method is a collection of statements that are performed when the method is executed.
method body
Which Scanner class method reads an int?
nextInt()
Which of the following values can be passed to a method that has an int parameter variable? float double long all of these none of these
none of these
You use this method to determine the number of items stored in an ArrayList object.
numberItems
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 < 100 || number > 500) { System.out.print("Enter another number: "); number = keyboard.nextInt(); }
numbers in the range 100-500
If numbers is a two-dimensional array, which of the following would give the length of row r?
numbers[r].length
The lifetime of a method's local variable is
only while the method is executing
Which of the following is included in a method call?
parentheses
In the header, the method name is always followed by this:
parenthesis
This type of loop will always be executed at least once.
post-test loop
Which of the following is a correct method header for receiving a two-dimensional array as an argument?
public static void passArray(int[2]) B) public static void passArray(int [][])
This ArrayList class method deletes an item from an ArrayList.
remove
Which of the following is NOT part of a method call?
return type
This is a sum of numbers that accumulates with each iteration of a loop
running total
Which of the following is NOT a part of the method header?
semicolon
This is a value that signals when the end of a list of values has been reached.
sentinel
You can use this ArrayList class method to replace an item at a specific location in an ArrayList.
set
Which of the following would be a valid method call for the following method? public static void showProduct (int num1, double num2) { int product; product = num1 * (int)num2; System.out.println("The product is " + product); }
showProduct(10, 4.5);
Character literals are enclosed in ________; string literals are enclosed in ________.
single quotes; double quotes
Another term for programs is:
software
What do you call the number that is used as an index to pinpoint a specific element within an array?
subscript
In order to do a binary search on an array:
the array must first be sorted in ascending order
This indicates the number of elements, or values, the array can hold.
the array's size declarator
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 < 100 && number > 500) { System.out.print("Enter another number "); number = keyboard.nextInt(); }
the boolean condition can never be true
When an individual element of an array is passed to a method:
the method does not have direct access to the original array
A parameter variable's scope is:
the method in which the parameter is declared
When you pass an argument to a method, be sure that the argument's data type is compatible with:
the parameter variable's data type
The phrase divide and conquer is sometimes used to describe:
the process of breaking a problem down into smaller pieces
In an if/else statement, if the boolean expression is false:
the statement or block following the else is executed
What will be the value of x after the following code is executed? int x = 10; while (x < 100); { x += 10; }
this is an infinite loop
A file must always be opened before using it and closed when the program is finished using it.
true
A parameter variable's scope is the method in which the parameter is declared.
true
A value-returning method can return a reference to a non-primitive type
true
A variable's scope is the part of the program that has access to the variable.
true
Although the dollar sign is a legal identifier character, you should not use it because it is normally used for special purposes.
true
An important style rule you should adopt for writing if statements is to write the conditionally executed statement on the line after the if statement.
true
Any method that calls a method with a throws clause in its header must either handle the potential exception or have the same throws clause.
true
Application software refers to programs that make the computer useful to the user.
true
Because the && operator performs short-circuit evaluation, your boolean expression will usually execute faster if the subexpression that is most likely false is on the left of the && operator.
true
Constants, variables, and the values of expressions may be passed as arguments to a method.
true
Each byte is assigned a unique number known as an address.
true
If the compiler encounters a statement that uses a variable before the variable is declared, an error will result.
true
Java provides a set of simple unary operators designed just for incrementing and decrementing variables.
true
Methods are commonly used to break a problem into small manageable pieces.
true
Named constants are initialized with a value, that value cannot be changed during the execution of the program.
true
No statement outside the method in which a parameter variable is declared can access the parameter by its name.
true
Programming style includes techniques for consistently putting spaces and indentation in a program so visual cues are created.
true
The Java Virtual Machine is a program that reads Java byte code instructions and executes them as they are read
true
The computer is a tool used by so many professions that it cannot be easily categorized
true
The do-while loop must be terminated with a semicolon.
true
The expression in a return statement can be any expression that has a value.
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 while loop has two important parts: (1) a boolean expression that is tested for a true or false value, and (2) a statement or block of statements that is repeated as long as the expression is true.
true
Two general categories of methods are void methods and value returning methods
true
When testing for character values, the switch statement does not test for the case of the character.
true
When the continue statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.
true
When you open a file with the PrintWriter class, the class can potentially throw an IOException.
true
When you pass the name of a file to the PrintWriter constructor, and the file already exists, it will be erased and a new empty file with the same name will be created.
true
Without programmers, the users of computers would have no software, and without software, computers would not be able to do anything.
true
You can use the PrintWriter class to open a file for writing and write data to it.
true
You must have a return statement in a value-returning method.
true
A Boolean expression is one that is either:
true or false
The expression tested by an if statement must evaluate to:
true or false
A flag may have the values:
ture or false
This type of loop allows the user to decide the number of iterations
user controlled loop
The sequential search algorithm
uses a loop to sequentially step through an array, starting with the first element
This type of method performs a task and sends a value back to the code that called it.
value-returning
In Java, ________ must be declared before they can be used
variables
This type of method performs a task and then terminates.
void
Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached?
while (inputFile.hasNext()) { ... }
Which of the following are pre-test loops?
while, for
The binary search algorithm:
will cut the portion of the array being searched in half each time the loop fails to locate the search value
What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++;
x = 33, y = 9
What will be printed when the following code is executed? int y = 10; if ( y == 10) { int x = 30; x += y; } System.out.print("x = "); System.out.print(x);
x is unknown when the last statement is executed
Which of the following expressions will determine whether x is less than or equal to y?
x<=y
Which one of the following is the not equal operator?
!=
Which of the following is NOT a valid comment statement?
*/ comment 3 /*
The increment operator is:
++
Subscript numbering always starts at what value?
0
If final int SIZE = 15 and int[] x = new int[SIZE], what would be the range of subscript values that could be used with x[]?
0 through 14
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100);
1
What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 10; }
100
int bonus, sales = 10000; if (sales < 5000) bonus = 200; else if (sales < 7500) bonus = 500; else if (sales < 10000) bonus = 750; else if (sales < 20000) bonus = 1000; else bonus = 1250;
1000
How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!!!");
11
What does the following code display? double x = 12.3798146; System.out.printf("%.2f\n", x);
12.38
What is the value of x after the following code has been executed? int x = 75; int y = 90; if ( x != y) x += y;
165
What will be returned from the following method? public static double methodA() { double a = 8.5 + 9.5; return a; }
18.0
If str1 and str2 are both Strings, which of the following expressions will correctly determine whether they are equal? (1)(str1 == str2) (2) str1.equals(str2) (3)(str1.compareTo(str2) == 0)
2 and 3
What would be the value of bonus after the following statements are executed? int bonus, sales = 1250; if (sales > 1000) bonus = 100; if (sales > 750) bonus = 50; if (sales > 500) bonus = 25; else bonus = 0;
25
What will be the value of x after the following code is executed?
28
If str1 and str2 are both Strings, which of the following will correctly test to determine whether str1 is less than str2? (1) (str1 < str2) (2) (str1.equals(str2) < 0) (3) (str1.compareTo(str2) < 0)
3
What will be the value of x[1] after the following code is executed? int[] x = {22, 33, 44}; arrayProcess(x); ... public static void arrayProcess(int[] a) { for(int k = 0; k < 3; k++) { a[k] = a[k] + 5; } }
38
What will be the value of x after the following code is executed? int x = 10; for (int y = 5; y < 20; y +=5) x += y;
40
int x = 11; do { x += 20; } while (x <= 100);
5
What will be printed after the following code is executed? for (int number = 5; number <= 15; number +=3) System.out.print(number + ", ");
5,8,11,14
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 = 20; for(int i = 0; i < SUB; i++) { x[i] = y; y += 5; }
60
17 % 3 * 2 - 12 + 15
7
A byte is a collection of:
8 bits
When writing the documentation comments for a method, you can provide a description of each parameter by using a:
@param tag
To do a case insensitive compare which of the following could be used to test the equality of two strings, str1 and str2?
A and B
Which of the following is not part of the programming process?
All of the above (Design, Testing, Debugging)
Which of the following is a secondary storage device?
All of the above (Hard drives, floppy disks, USB drives)
The major components of a typical computer system consist of:
All of these
What do you normally use with a partially-filled array?
An accompanying integer value that holds the number of items stored in the array
What will be the result of executing the following code? int[] x = {0, 1, 2, 3, 4, 5};
An array of 6 values ranging from 0 through 5 and referenced by the variable x will be created.
What will be returned from the following method? public static float[] getValue(int x)
An array of float values
What will be the results of the following code? final int ARRAY_SIZE = 5; float[] x = float[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; }
An error will occur when the program runs.
Each repetition of a loop is known as what?
An iteration
These operators use two operands:
Binary
Computer programming is: An art A science Both of the above neither of the above
Both of the above
An operating system can be categorized according to:
Both the number of users that can be accommodated, and the number of tasks they can perform at one time
If you are using a block of statements, don't forget to enclose all of the statements in a set of:
Braces
Any method that calls a method with a throws clause in its header must: A. handle the potential exception B. have the same throws clause C. both A and B D. do nothing, the called program will take care of the throws clause
C. Both A and B
In the programming process which of the following is NOT involved in defining what the program is to do:
Compile code
If method A calls method B, and method B calls method C, and method C calls method D, when method D finishes, what happens?
Control is returned to method C.
A loop that repeats a specific number of times is known as a(n):
Counter controlled loop
Local variables: A. are hidden from other methods B. may have the same name as local variables in other methods C. lose the values stored in them between calls to the method in which the variable is declared D. All of these
D. All of these
results in only the object's methods being able to directly access and make the changes to the object's data.
Data hiding
Which one of the following would contain the translated Java byte code for a program named Demo?
Demo.class
What is wrong with the following method call? displayValue (double x);
Do not include the data type in the method call.
Which one of the following methods would you use to convert a string to a double?
Double.ParseDouble
A sorting algorithm is used to locate a specific item in a larger collection of data.
False
An array can hold multiple values of several different data types simultaneously
False
If a[] and b[] are two integer arrays, the expression a == b compares the array contents.
False
In a switch statement, if two different values for the CaseExpression would result in the same code being executed, you must have two copies of the code, one after each CaseExpression.
False
Java limits the number of dimensions that an array may have to 15.
False
Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents?
FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter);
It is common practice to use a ________ variable as a size declarator.
Final
This is a boolean variable that signals when some condition exists in the program:
Flag
What will be displayed as a result of executing the following code? int x = 8; String msg = "I am enjoying java."; String msg1 = msg.toUpperCase(); String msg2 = msg.toLowerCase(); char ltr = msg.charAt(x); int strSize = msg.length(); System.out.println(msg); System.out.println(msg1); System.out.println(msg2); System.out.println("Character at index x = " + ltr); System.out.println("msg has " + strSize + " characters.");
I am enjoying java. I AM ENJOYING JAVA. i am enjoying java. Character at index x = o msg has 19 characters.
Which of the following is NOT a rule that must be followed when naming identifiers?
Identifiers can contain spaces.
________ is the process of inspecting data given to the program by the user and determining if it is valid
Input validation
The computer can do such a wide variety of tasks because:
It can be programmed
This is a control structure that causes a statement or group of statements to repeat.
Loop
This variable controls the number of times that the loop iterates.
Loop control variable
Suppose you are at an operating system command line, and you are going to use the following command to compile a program: javac MyClass.java Before entering the command, you must:
Make sure you are in the same directory or folder where MyClass.java is located
The switch statement is a:
Multiple alternative decision structure
What will be displayed after the following statements have been executed? final double x; x = 54.3; System.out.println("x = " + x );
Nothing, this is an error.
The original name for Java was:
Oak
A special variable that holds a value being passed into a method is called what?
Parameter
One of the design tools used by programmers when creating a model of the program is:
Pseudocode
When a computer is running a program, the CPU is engaged in a process formally known as:
The fetch/decode/execute cycle
What would be the results of the following code? final int SIZE = 25; int[] array1 = new int[SIZE]; ... // Code that will put values in array1 int value = 0; for (int a = 0; a <= array1.length; a++) { value += array1[a]; }
This would cause the program to crash.
The purpose of validating the results of the program is:
To determine whether the program solves the original problem
A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.
True
An ArrayList object automatically expands in size to accommodate the items stored in it.
True
Any items typed on the command-line, separated by space, after the name of the class are considered to be one or more arguments that are to be passed into the main method.
True
Declaring an array reference variable does not create an array.
True
In a switch statement, each of the case values must be unique.
True
Java does not limit the number of dimensions that an array may have.
True
Objects in an array are accessed with subscripts, just like any other data type in an array.
True
The String.format method works exactly like the System.out.printf method, except that it does not display the formatted string on the screen.
True
The String[] args parameter in the main method header allows the program to receive arguments from the operating system command-line.
True
The System.out.printf method formats a string and displays it in the console window.
True
To compare the contents of two arrays, you must compare the elements of the two arrays.
True
Unicode is an international encoding system that is extensive enough to represent ALL the characters of ALL the world's alphabets.
True
When an array of objects is declared, but not initialized, the array values are set to null.
True
This is an international coding system that is extensive enough to represent all the characters of all the world's alphabets:
Unicode
In Java, when a character is stored in memory, it is actually stored as a(n):
Unicode number
What would be the results of the following code? int[] array1 = new int[25]; ... // Code that will put values in array1 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.
If you prematurely terminate an if statement with a semicolon, the compiler will: not display and error message assume you are placing a null statement there both of these
both of these
A block of code is enclosed in a set of:
braces { }
Methods are commonly used to:
break a problem down into small manageable pieces
A loop that executes as long as a particular condition exists is called a(n):
conditional loop
In memory, an array of String objects:
consists of elements, each of which is a reference to a String object
Variables are classified according to their:
data type
To create a method you must write its:
definition
This is an item that separates other items.
delimiter
Byte code instructions are:
Read and interpreted by the JVM
This type of operator determines whether a specific relationship exists between two values:
Relational
Syntax is:
Rules that must be followed when writing a program
These are used to indicate the end of a Java statement.
Semicolons
Before entering a loop to compute a running total, the program should first do this.
Set the accumulator where the total will be kept to an initial value, usually zero
________ works like this: If the expression on the left side of the && operator is false, the expression the right side will not be checked.
Short-circuit evaluation
Software refers to
Software refers to
Which of the following is NOT a primitive data type?
String
When the + operator is used with strings, it is known as the:
String concatenation operator
Java was developed by:
Sun Microsystems
Variables are:
Symbolic names made up by the programmer that represents locations in the computer's RAM
Which of the following will format 12.7801 to display as $12.78?
System.out.printf("$%,.2f", 12.7801);
Which of the following will format 12.78 to display as 12.8%?
System.out.printf("%.1f%%", 12.78);
To print "Hello, world" on the monitor, use the following Java statement:
System.out.println("Hello, world");
You can use this method to determine whether a file exists.
The File class's exists method
A program is a sequence of instructions stored in:
The computer's memory
Internally, the central processing unit (CPU) consists of two parts:
The control unit and the arithmetic and logic unit (ALU)
The header of a value-returning method must specify this.
The data type of the return value
Enclosing a group of statements inside a set of braces creates a:
block of statements
When a method tests an argument and returns a true or false value, it should return:
a boolean value
In a general sense, a method is:
a collection of statements that performs a specific task
A ragged array is:
a two-dimensional array where the rows are of different lengths
In the following code, System.out.println(num) is an example of: double num = 5.4; System.out.println(num); num = 0.0;
a void method
In all but rare cases, loops must contain within themselves
a way to terminate
What would be the results of the following code? int[] x = { 55, 33, 88, 22, 99, 11, 44, 66, 77 }; int a = 10; if(x[2] > x[5]) a = 5; else a = 8;
a=5
When an object, such as a String, is passed as an argument, it is:
actually a reference to the object that is passed
This ArrayList class method is used to insert an item into an ArrayList.
add
A for loop normally performs which of these steps?
all of these
Breaking a program down into small manageable methods: makes problems more easily solved allows for code reuse simplifies programs all of these
all of these
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 all of these
all of these
Which of the following is NOT a benefit derived from using methods in programming?
all of these
A value-returning method must specify this as its return type in the method header.
an int
What will be 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 { ans = y + 10; y += x; }
ans = 35, x = 15, y = 40
What will be the values of ans, x, and y after the following statements are executed? int ans = 35, x = 50, y =50; if ( x >= y) { ans = x + 10; x -=y; } else { ans = y + 10; y += x; }
ans = 60, x =0, y =50
All @param tags in a method's documentation comment must:
appear after the general description of the method
Values that are sent into a method are called:
arguments