Programming 2 - Exam 1
Q063 Which operator is used to invert a conditional statement? Which of the following operators is used to invert a conditional statement?
!
Q041 Which operator computes the remainder of an integer division? Which one of the following operators computes the remainder of an integer division?
%
Q061 Which operator is used to combine two Boolean conditions? Which of the following operators is used to combine two Boolean conditions?
&&
Q021 Which condition avoids division by zero? Which condition, when supplied in the if statement below in place of (. . .), will correctly protect against division by zero? if (. . .) { result = grade / num; System.out.println("Just avoided division by zero!"); }
(num != 0)
Q005 What is the range of index values for an array of size 10?1 pts What is the valid range of index values for an array of size 10?
0 to 9
Q083 What is the size of the given array list after add(10.5)?1 pts Consider the following code snippet: ArrayList<Double> somedata = new ArrayList<Double>(); somedata.add(10.5); What is the size of the array list somedata after the given code snippet is executed?
1
Q001 How many iterations of while loop? How many times will the following loop run? int i = 0; while (i < 10) { System.out.println(i); i++; }
10
Q027 How many times does for loop execute? How many times does the loop execute in the following code fragment? int i; for (i = 0; i < 50; i = i + 4) { System.out.println(i); }
13
Q008 How many times does while loop display result? How many times does the code snippet below display "Hello"? int i = 0; while (i != 15) { System.out.println("Hello"); i++; }
15
Q063 How many elements can be stored in an array of dimension 5 by 6?1 pts How many elements can be stored in a two-dimensional 5 by 6 array?
30
Q004 What are values of the elements at the given array indexes?1 pts What is the output of the following code snippet?
3040
Q042 What does every statement end with? Every statement in Java must be terminated with ____________.
;
Q033 Which is true about method return statements? Which of the following is true about method return statements?
A method can hold multiple return statements, but only one return statement executes in one method call.
Q018 Which of the following guidelines will make code more explanatory for others? Which of the following guidelines will make code more explanatory for others?
Add comments to source code
Q048 Which statement is true about passing arrays to a method?1 pts Which one of the following statements is true about passing arrays to a method?
By default, arrays are passed by reference to a method.
Q017 Which translates high-level descriptions into machine code? Which one of the following translates high-level descriptions into machine code?
Compiler
Q058 What to use for rounding number? One way to avoid round-off errors is to use:
Convert double values to ints and do calculations using integers.
Q047 Which statement is true about the main method? Which of the following statements is true with respect to the main method in Java?
Every Java application must have a main method.
Q025 What can be used as an argument in a method call? What can be used as an argument in a method call? I. A variable II. An expression III. Another method call that returns a value IV. Another method call that has no return value
I, II, and III
Q007 Which statements are true about array references?1 pts Which statements are true about array references? I. Assigning an array reference to another creates a second copy of the array. II. An array reference specifies the location of an array. III. Two array references can reference the same array.
II and III only
Q018 Which statements about the enhanced for loop are true?1 pts Which statements about the enhanced for loop are true? I. It is suitable for all array algorithms. II. It does not allow the contents of the array to be modified. III. It does not require the use of an index variable.
II, III only
Q062 Which describes the process of stepwise refinement? Which of the following options describes the process of stepwise refinement? I. Using arguments to pass information to a method II. Using unit tests to test the behavior of methods III. Decomposing complex tasks into simpler ones
III
Q015 What should be done to improve (program that does same calculation repeatedly)? In an accounting application, you discover several places where the total profit, a double value, is calculated. Which of the following should be done to improve the program design? I. The next time the total profit is calculated, use copy and paste to avoid making coding errors. II. Provide the same comment every time you repeat the total profit calculation. III. Consider writing a method that returns the total profit as a double value.
III.
Q008 Which statement(s) is true about an if statement? Which of the following statements is (are) true about an if statement? I. It guarantees that several statements are always executed in a specified order. II. It repeats a set of statements as long as the condition is true. III. It allows the program to carry out different actions depending on the value of a condition.
III. It allows the program to carry out different actions depending on the value of a condition.
Q098 How should methods be invoked on string variables? What is the correct way to invoke methods on variables in Java that are strings?
Invoke them using the variable name and the dot (.) notation.
Q001 Which is correct about a local variable? Which of the following is correct about a local variable?
It is declared within the scope of any method
Q010 What is the error in this if statement? The following code snippet contains an error. What is the error? if (cost > 100); { cost = cost - 10; } System.out.println("Discount cost: " + cost);
Logical error: if statement has do-nothing statement after if condition
Q035 What is the value of Math.abs(-2)?
Math.abs() is the absolute value of a number. Math.abs(-2) = 2
Q006 Which is true about methods? Which of the following is true about methods?
Methods can have multiple arguments and can return one return value.
Q010 Which is not legal in a method definition? Which of the following is not legal in a method definition?
Multiple return values
Q088 What is the scope of local variable What is the output of the following code snippet? public static void main(String[] args) { int gvar = 0; gvar = gvar + 10; if (gvar > 0) { int lvar = gvar + 1; } System.out.println(lvar); }
No output due to compilation error
Q037 Method return with multi-alternative "if" statement. What is wrong with the following code? public static String grade(int score) { if (score >= 9) { return A; } else if (score >= 8) { return B; } else if (score >= 6) { return C; } else if (score >= 4) { return D; } return F; }
No return statement for all branches of "if" statement
Q034 Are these lines of code the same? Suppose you examine a simple Java program and the first line is public class HelloPrinter. Is this the same thing in Java as the line public class helloprinter?
No, because Java is case-sensitive, these are considered to be completely distinct.
Q009 Which is correct (about variable names)? Consider the following Java variable names: I. 1stInstance II. basicInt% III. empName_ IV. addressLine1 V. DISCOUNT Which of the following options represent valid Java variable names?
Only III, IV, and V are valid Java variable names.
Q002 Why is the term "black box" used with methods? The term "Black Box" is used with methods because
Only the specification matters; the implementation is not important
Q009 What are the parts of a method header? After the keywords "public static", what are the names (in order) of the parts of this method header? public static int myFun (double a)
Return type, method name, parameter variable type, parameter variable name
Q100 What type of error with code containing Integer.parseInt()? What (if any) type of error occurs with the following code if the user input is ABC? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String str = in.next(); int count = Integer.parseInt(str); System.out.println("Input is " + count); }
Run-time error
Q027 What statement provides the desired output string from array?1 pts Consider the following code snippet. Which statement should be used to fill in the empty line so that the output will be [32, 54, 67.5, 29, 35]? public static void main(String[] args) { double data[] = {32, 54, 67.5, 29, 35}; ______________ System.out.println(str); }
String str = Arrays.toString(data);
Q001 Which code displays the 28th element of somearray?1 pts Consider the following line of code: int[] somearray = new int[30]; Which one of the following options is a valid line of code for displaying the twenty-eighth element of somearray?
System.out.println(somearray[27]);
Q080 Which operator is used to concatenate two or more strings?
The ampersand symbol (&) is the recommended concatenation operator. It is used to bind a number of string variables together, creating one string from two or more individual strings.
Q015 What is the error in this array code fragment?1 pts What is the error in this array code fragment? double[] data; data[0] = 15.25;
The array referenced by data has not been initialized.
Q017 What is the result of the enhanced for loop?1 pts What is the result of the following code? for (double element : values) { element = 0; }
The array values is not modified.
Q002 Which statement is true about the if statement? Which of the following statements is true about the if statement?
The else block is optional.
Q042 What happens to the fractional part (in integer division)? What happens to the fractional part when a division is performed on two integer variables?
The fractional part is discarded
Q013 Which storage type provides data persistence without electricity? Which of the following typically provides data persistence without electricity?
The hard disk & Secondary storage
Q012 What is the error in this method definition? What is the error in the following method definition? public static int tripler(int numPara) { double result = numPara * 3; }
The method does not return a value.
Q078 How do you compute the length of the string str?
The method for computing length of a string str in java is str.length()
Q047 What is true about method that uses array?1 pts Consider the following method: public static int mystery(int length, int n) { int[] result = new int[length]; for (int i = 0; i < result.length; i++) { result[i] = (int) (n * Math.random()); } return result; }
The method return type should be changed to int[]
Q056 What is a good rule for deciding the number of statements in a method?
The method should perform only one task and contain just enough statements for the task.
Q082 What is the result of this definition of an array list?1 pts What is the result of the following definition of an array list? ArrayList<Double> points;
The statement defines an array list reference but leaves its value as null.
Q034 What is problem with method? What is the problem with the definition of the following method that calculates and returns the tax due on a purchase amount? public static double taxDue(double amount, double taxRate) { double taxDue = 0.0; taxDue = amount * taxRate; }
The taxDue method does not return a value.
Q067 Which value of the 2-dimensional array arr is stored in arr[0][2]?1 pts Consider the following code snippet: int val = arr[0][2]; Which value of arr is stored in the val variable?
The value in the first row and the third column
Q017 What is the error in this method definition? What is the syntax error in the following method definition? public static String parameter(double r) { double result; result = 2 * 3.14 * r; return result; }
The value that is returned does not match the specified return type
Q025 Which statement about Boolean conditions is true? When hand-tracing a portion of code, which statement about Boolean conditions is true?
They are crucial to evaluate since they determine if-statement conditions and looping.
Q007 Which statement about variable names is true? Which statement is true?
They can contain an underscore symbol ("_")
Q080 What is the purpose of writing a stub method?
To test another method without writing all the implementation details of a method called by the method being tested
Q015 Which statement is correct about constants? Which of the following statements is correct about constants?
You can make a variable constant by using the "final" reserved word when declaring it.
Q012 Which is an assignment statement? Which one of the following is an assignment statement?
a = 20;
Q079 What is a stub method? A stub method is ___________________.
a method that acts as a placeholder and returns a simple value so another method can be tested
Q010 What is an example of an input device that interfaces between humans and computers? An example of an input device that interfaces between computers and humans is ____________.
a microphone.
Q040 Which code is correct to grow the size of an array?1 pts Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; Using Java 6 or later, which statement grows the data array to twice its size?
a) data = Arrays.copyOf(data, 2 * data.length);
Q032 What kinds of tools are included in an IDE? An integrated development environment (IDE) bundles tools for programming into a unified application. What kinds of tools are usually included?
an editor and a compiler
Q026 Which option is the best variable name? Which option represents the best choice for a variable name to represent the average grade of students on an exam?
averageGrade
Q023 Software needed to translate Java source code. In order to translate a Java program to a class file, the computer needs to have software called a(n) _______________.
compiler
Q055 What do the diamond-shaped boxes indicate in a flow chart? The flow chart shows the order in which steps should be executed, and the diamond-shaped boxes indicate ______________.
conditional tests
Q037 What is a Java "class" file? A Java "class" file _______________________.
containing bytecode that can be executed on the Java Virtual Machine
Q038 Which code is correct to copy from one array to another?1 pts Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; String [] data2; In Java 6 and later, which statement copies the data array to the data2 array?
data2 = Arrays.copyOf(data, data.length);
Q039 Which loop executes before checking condition? Which of the following loops executes the statements inside the loop before checking the condition?
do
Q007 Which statement DOES NOT correctly compute the discount? A store provides 10 percent discount on all items with a price of at least $100. No discount is otherwise applicable. Which of the following DOES NOT correctly compute the discount?
double discount = 0.10 * price; if (price <= 100) { discount = 0; }
Q044 Choose the loop that is equivalent to this loop. int n = 1; double x = 0; double s; do { s = 1.0 / (n * n); x = x + s; n++; } while (s > 0.01);
double x = 0; double s = 1; for (int k = 1; s > 0.01; k++) { s = 1.0 / (k * k); x = x + s; }
Q037 Which word defines the branch to be executed when none of the conditions are true? Consider a situation where multiple if statements are combined into a chain to evaluate a complex condition. Which of the following reserved words is used to define the branch to be executed when none of the conditions are true?
else
Q016 Which statement defines a constant? Which one of the following statements defines a constant with the value 123?
final int MY_CONST = 123;
Q029 Which declares a float variable? Which of the following options declares a float variable?
float age;
Q016 What is the correct enhanced for loop to iterate over the array1 pts Complete the following code snippet with the correct enhanced for loop so it iterates over the array without using an index variable. String[] arr = { "abc", "def", "ghi", "jkl" }; ___________________ { System.out.print(str); }
for (String str : arr)
Q033 Which of the following for loops is illegal? Which of the following for loops is illegal?
for (int i = 0) { }
Q039 What does the last element of data2 contain after an array copy?1 pts Consider the following code snippet in Java 6 or later: String[] data = { "abc", "def", "ghi", "jkl" }; String[] data2 = Arrays.copyOf(data, data.length - 1); What does the last element of data2 contain?
ghi
Q023 What condition evaluates to true if the length of a string s1 is odd? What is the conditional required to check whether the length of a string s1 is odd?
if ((s1.length() % 2) != 0)
Q004 Which is the correct syntax for an if statement?
if (expression)
Q009 Which condition for an if statement is correct? Suppose one needs an if statement to check whether an integer variable pitch is equal to 440 (which is the frequency of the note "A" to which strings and orchestras tune). Which condition is correct?
if (pitch == 440)
Q030 Which condition tests for single digit input? Which of the following conditions tests whether the user enters the single digit 5? String s; Scanner in = new Scanner(System.in); System.out.print("Enter a single digit: "); s = in.next();
if (s.equals("5"))
Q066 Which condition tests for the string "Hello"? Which of the following conditions tests for the user to enter the string "Hello"? String s; Scanner in = new Scanner(System.in); System.out.print("Enter a word: "); s = in.next();
if (s.equals("Hello"))
Q001 Which defines an integer variable? Which of the following options defines an integer variable?
int age;
Q002 Which correctly defines and initializes an integer variable value? Which one of the following is a correct method for defining and initializing an integer variable with name value?
int value = 30;
Q002 Which statement defines integer array numarray with ten elements?1 pts Identify the correct statement for defining an integer array named numarray of ten elements.
int[] numarray = new int[10];
Q003 Which array initialization statement is invalid?1 pts Which one of the following statements is a valid initialization of an array named somearray of ten elements?
int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Q027 What's wrong with integer expression? What is wrong with the following code? int count = 2000 * 3000 * 4000;
integer overflow
Q007 What is the function of a CPU? The Central Processing Unit is primarily responsible for ______________.
locating and executing the program instructions / fetches data from external memory or devices and places processed data into storage.
Q041 What is a switch statement in Java? The switch statement in Java ________________.
multi way branch statement
Q062 Which is correct definition for initializing a 2-dimensional array of three rows and two columns?1 pts Which one of the following is the correct definition for initializing data in a two-dimensional array of three rows and two columns?
nt[][] arr ={{ 1, 1 },{ 2, 2 },{ 3, 3 }};
Q031 What is the reason for the ranges for integer values in Java? The typical ranges for integers may seem strange but are derived from ___________.
powers of two because of base 2 representation within the computer
Q024 Which is best choice for declaring method that calculates volume of cuboid? Suppose you need to write a method that calculates the volume of a 3D rectangular solid. Which of the following is the best choice for the declaration of this method?
public static double volume(double w, double h, double l)
Q073 Which is NOT a good practice in developing a computer program? Which of the following is NOT a good practice when developing a computer program?
put as many statements as possible in the main method
Q024 A Java virtual machine is ___ .
software
Q036 Where is Java source code stored? The source code for a Java program is stored in a file ________________.
that ends with a .java suffix
Q041 What happens when an array runs out of space?1 pts When an array reading and storing input runs out of space, __________________.
the array must be "grown" using the new command and the copyOf method.
Q047 What is the purpose of methods without a return value? The purpose of a method that returns void is __________________.
to package a repeated task as a method even though the task does not yield a value
Q022 Software needed to run Java on a computer in order to run Java programs on a computer, the computer needs to have software called a(n) __________.
virtual machine
Q081 Which statement gets an element from the array list?1 pts The following statement gets an element from position 4 in an array: x = a[4]; What is the equivalent operation using an array list?
x = a.get(4);
Q016 Insert appropriate code in while loop. Select the statement that correctly completes the loop in this code snippet. int years = 20; double balance = 10000; while (years > 0) { __________ double interest = balance * rate / 100; balance = balance + interest; }
years--;