Download Loops and Methods in Java and more Exams Java Programming in PDF only on Docsity!
Java Final Exam Multiple Choice 100% Accurate!!
- The increment operator is: A) ++ B) -- C) *= D) -= - ANSWERAnswer: A
- What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++; A) x = 25, y = 8 B) x = 33, y = 8 C) x = 33, y = 9 D) x = 34, y = 9 - ANSWERAnswer: C
- What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z); A) 24 B) 28 C) 30 D) 35 - ANSWERAnswer: B
- This is a control structure that causes a statement or group of statements to repeat. A) Block B) Loop C) Prefix mode D) Body - ANSWERAnswer: B
- If a loop does not contain within itself a way to terminate, it is called a(n): A) while loop B) do-while loop C) for loop D) infinite loop - ANSWERAnswer: D
- Each repetition of a loop is known as what? A) An iteration B) A cycle C) An execution D) A Lap - ANSWERAnswer: A
- This variable controls the number of times that the loop iterates. A) Counter variable B) Loop control variable C) Running total D) Decrement variable - ANSWERAnswer: B
- This type of loop will always be executed at least once. A) pre-test loop B) post-test loop C) sentinel loop D) for loop - ANSWERAnswer: B
- If you are using a block of statements, don't forget to enclose all of the statements in a set of: A) Braces B) Double quotes C) Semicolons D) Parentheses - ANSWERAnswer: A
- What will be the value of x after the following code is executed?
B) Dynamically executed loop C) User controlled loop D) Infinite loop - ANSWERAnswer: C
- 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(); } A) Numbers less than 100 or greater than 500 B) Numbers in the range 100 - 499 C) Numbers in the range 100 - 500 D) The boolean condition can never be true. - ANSWERAnswer: D
- What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x > 5); A) 10 B) 200 C) This is an infinite loop. D) The loop will not be executed, the initial value of x > 5. - ANSWERAnswer: C
- How many times will the following do-while loop be executed?
int x = 11; do { x += 20; } while (x > 100); A) 0 B) 1 C) 4 D) 5 - ANSWERAnswer: B
- A loop that repeats a specific number of times is known as a(n): A) sentinel loop B) conditional loop C) counter-controlled loop D) infinite loop - ANSWERAnswer: C
- How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!!!"); A) 1 B) 10 C) 12 D) 0 - ANSWERAnswer: C
- 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; A) 40 B) 25 C) 30
PrintWriter outFile = new PrintWriter(fwriter); B) FileWriter fwriter = new FileWriter("MyFile.txt"); PrintWriter outFile = new PrintWriter(fwriter); C) PrintWriter outfile = new PrintWriter("MyFile.txt", true); D) PrintWriter outfile = new PrintWriter(true, "MyFile.txt"); - ANSWERAnswer: A
- 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? A) while (inputFile != null) { ... } B) while (!inputFile.EOF) { ... } C) while (inputFile.hasNext()) { ... } D) while (inputFile.nextLine == " ") { ... } - ANSWERAnswer: C
- What will be the values of x and y as a result of the following code? int x = 12, y = 5; x += y--; A) x = 12, y = 5 B) x = 16, y = 4 C) x = 17, y = 5 D) x = 17, y = 4 - ANSWERAnswer: D
- Methods are commonly used to: A) speed up the compilation of a program B) break a problem down into small manageable pieces C) emphasize certain parts of the logic D) document the program - ANSWERAnswer: B
- Which of the following is NOT a benefit derived from using methods in programming? A) Pproblems are more easily solved. B) simplifies programs C) code reuse D) All of the above are benefits. - ANSWERAnswer: D
- This type of method performs a task and sends a value back to the code that called it. A) value-returning B) void C) complex D) local - ANSWERAnswer: A
- In the following code, System.out.println(num) is an example of: double num = 5.4; System.out.println(num); num = 0.0; A) a value-returning method B) a void method C) a complex method D) a local variable - ANSWERAnswer: B
- To create a method you must write its: A) header B) return type C) body D) definition - ANSWERAnswer: D
- In the header, the method name is always followed by this: A) parentheses B) return type
B) its value may be changed within the called method C) values may not be passed to methods D) the method must not assign another value to the parameter that receives the argument - ANSWERAnswer: A
- What is wrong with the following method call? displayValue (double x); A) There is nothing wrong with the statement. B) displayValue will not accept a parameter. C) Do not include the data type in the method call. D) x should be a String. - ANSWERAnswer: C
- Given the following method header, which of the method calls would be an error? public void displayValues(int x, int y) A) displayValue(a,b); // where a is a short and b is a byte B) displayValue(a,b); // where a is an int and b is a byte C) displayValue(a,b); // where a is a short and b is a long D) They would all give an error. - ANSWERAnswer: C
- 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); } A) showProduct(5.5, 4.0); B) showProduct(10.0, 4); C) showProduct(10, 4.5); D) showProduct(33.0, 55.0); - ANSWERAnswer: C
- When an object, such as a String, is passed as an argument, it is: A) actually a reference to the object that is passed B) passed by value like any other parameter value C) encrypted D) necessary to know exactly how long the string is when writing the program - ANSWERAnswer: A
- All @param tags in a method's documentation comment must: A) end with a */ B) appear after the general description of the method C) appear before the method header D) span several lines - ANSWERAnswer: B
- A special variable that holds a value being passed into a method is called what? A) Modifier B) Parameter C) Alias D) Argument - ANSWERAnswer: B
- When you pass an argument to a method, be sure that the argument's data type is compatible with: A) the parameter variable's data type B) the method's return type C) the version of Java currently being used D) IEEE standards - ANSWERAnswer: A
- A parameter variable's scope is: A) the method in which the parameter is declared B) the class to which the method belongs C) the main method D) All of the above - ANSWERAnswer: A
A) 18.
B) 18 (as an integer) C) 8 D) This is an error. - ANSWERAnswer: A
- In a @return tag statement the description: A) cannot be longer than one line B) describes the return value C) must be longer than one line D) describes the parameter values - ANSWERAnswer: B
- When a method tests an argument and returns a true or false value, it should return: A) a zero for true and a one for false B) a boolean value C) a zero for false and a non-zero for true D) a method should not be used for this type test - ANSWERAnswer: B
- The phrase divide and conquer is sometimes used to describe: A) the backbone of the scientific method B) the process of dividing functions C) the process of breaking a problem down into smaller pieces D) the process of using division to solve a mathematical problem - ANSWERAnswer: C
- In a general sense, a method is: A) a plan B) a statement inside a loop C) a comment D) a collection of statements that performs a specific task - ANSWERAnswer: D
- Breaking a program down into small manageable methods: A) makes problems more easily solved
B) allows for code reuse C) simplifies programs D) all of the above - ANSWERAnswer: D
- This type of method performs a task and then terminates. A) value-returning B) void C) local D) simple - ANSWERAnswer: B
- In the following code, Integer.parseInt(str), is an example of: int num; string str = "555"; num = Integer.parseInt(str) + 5; A) a value-returning method B) a void method C) a local variable D) a complex method - ANSWERAnswer: A
- Which of the following is NOT a part of the method header? A) return type B) method name C) parentheses D) semicolon - ANSWERAnswer: D
- Which of the following is included in a method call? A) return type B) method modifiers C) parentheses D) return variable - ANSWERAnswer: C
C) displayValue(a,b); // where a is a short and b is a long D) They would all give an error. - ANSWERAnswer: C
- Which of the following would be a valid method call for the following method? public static void showProduct(double num1, int num2) { double product; product = num1 * num2; System.out.println("The product is " + product); } A) showProduct("5", "40"); B) showProduct(10.0, 4.6); C) showProduct(10, 4.5); D) showProduct(3.3, 55); - ANSWERAnswer: D
- When writing the documentation comments for a method, you can provide a description of each parameter by using a: A) @comment tag B) @doc tag C) @param tag D) @return tag - ANSWERAnswer: C
- Values stored in local variables: A) are lost between calls to the method in which they are declared B) retain their values from the last call to the method in which they are declared C) may be referenced by the calling method D) may be referenced by any other method, if the method in which they are declared is a public method - ANSWERAnswer: A
- Local variables can be initialized with:
A) constants B) parameter values C) the results of an arithmetic operation D) any of the above - ANSWERAnswer: D
- A value-returning method must specify this as its return type in the method header. A) an int B) a double C) a boolean D) any valid data type - ANSWERAnswer: D
- What will be returned from the following method? public static int methodA() { double a = 8.5 + 9.5; return a; } A) 18. B) 18 (as an integer) C) 8. D) This is an error. - ANSWERAnswer: D
- To document the return value of a method, use this in a documentation comment. A) The @param tag B) The @comment tag C) The @return tag D) The @returnValue tag - ANSWERAnswer: C
- The process of breaking a problem down into smaller pieces is sometimes called: A) divide and conquer B) scientific method
T/F: Constants, variables, and the values of expressions may be passed as arguments to a method. - ANSWERAnswer: TRUE T/F: A parameter variable's scope is the method in which the parameter is declared. - ANSWERAnswer: TRUE T/F: You must have a return statement in a value-returning method. - ANSWERAnswer: TRUE T/F: 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. - ANSWERAnswer: TRUE T/F: In the method header the static method modifier means the method is available to code outside the class. - ANSWERAnswer: FALSE T/F: Only constants and variables may be passed as arguments to methods. - ANSWERAnswer: FALSE
- No statement outside the method in which a parameter variable is declared can access the parameter by its name. - ANSWERAnswer: TRUE T/F: The expression in a return statement can be any expression that has a value. - ANSWERAnswer: TRUE T/F: A value-returning method can return a reference to a non-primitive type. - ANSWERAnswer: TRUE
- One or more objects may be created from a(n): A) field B) class C) method
D) instance - ANSWERAnswer: B
- Class objects normally have ________ that perform useful operations on their data, but primitive variables do not. A) fields B) instances C) methods D) relationships - ANSWERAnswer: C
- In the cookie cutter metaphor, think of the ________ as a cookie cutter and ________ as the cookies. A) object; classes B) class; objects C) class; fields D) attribute; methods - ANSWERAnswer: B
- Which of the following are classes from the Java API? A) Scanner B) Random C) PrintWriter D) All of the above - ANSWERAnswer: D
- When you are working with a ________, you are using a storage location that holds a piece of data. A) primitive variable B) reference variable C) numeric literal D) binary number - ANSWERAnswer: A
- What is stored by a reference variable? A) A binary encoded decimal B) A memory address