Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 111 Exam 2 Questions MULTIPLE CHOICE 1. When an object, such as a String, is passed as an argument, it is a. copied into another memory location, and the new location is given to the method b. encrypted for security c. actually a reference to the object that is passed d. necessary to know exactly how long the string is when writing the program e. A and B 2. 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. until the body of the next if statement or while/for loop e. None of the above 3. Local variables declared in a method a. are only available to other methods in the class b. may not 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. are only meant for use in the method they are declared e. C and D 4. When an argument is passed to a method, a. all public data members of the class are updated with the value of the argument b. the argument value can be changed within the called method c. it is invalid as arguments may not be passed to methods d. it is represented by the parameter variable in the method definition e. the method must not assign another value to the parameter that receives the argument 5. The header of a value-returning method must specify this. a. The method's local variable names b. The arguments required by the method c. The data type of the return value d. The class the method belongs to e. B and C 6. The phrase divide and conquer is sometimes used to describe a. the backbone of the scientific method b. the process of breaking a problem down into smaller pieces c. the process of dividing functions d. the process of using division to solve a mathematical problem e. the process of using different objects to hold different data 7. Breaking a program down into small manageable methods a. makes problems more easily solved b. allows for code reuse c. simplifies programs d. makes the code easier to read e. all of the above 8. In the following code, nextInt(), is an example of ________. int num; Scanner s = new Scanner(System.in); num = s.nextInt() + 2; a. b. c. d. e. 9. a value-returning method a void method a local variable a complex method None of the above Given the following method header, which of the method calls would be an error? public void displayValues(double x, int y) a. b. c. d. e. displayValue(a,b); // where a is a long and b is a short displayValue(a,b); // where a is an int and b is a byte displayValue(a,b); // where a is a short and b is a long they would all give an error none would give an error 10. The scope of a private instance field is: a. the instance methods of the same class b. inside the class, but not inside any method c. inside the parentheses of a method header d. the method in which they are defined e. C and D 11. 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 e. Both C and D 12. Local variables can be initialized with a. constants b. parameter values c. the results of an arithmetic operation d. any of the above e. only A and B 13. What is the output of the following code? int a = 2, b = 2; while (--a > 0 && b++ > 0){ System.out.println(a + “ “ + b); } a. 2 2 b. 1 3 c. 2 4 d. 2 2 e. It is an infinite loop 14. What is the output of the following code: int i = 10; for ( ; i < 5 ; ) System.out.println("This is a string!"); a. b. c. d. e. prints out This is a string! forever. prints out This is a string! four times. prints out This is a string! five times. this code cannot compile. does not print anything. 15. What is the output of the following code fragment? int x = 3; int y = 7; while (x < 5) { x++; y = --y * 2; } System.out.println(y); a. b. c. d. e. 4 7 10 22 28 16. How many “Java” are displayed from the following code fragment? int count = 0; while (count < 3) System.out.println(“Java”); count++; a. 0 b. 1 c. 2 d. e. 3 Infinite 17. How many “TAMU” are displayed from the following code fragment? for (int count = 0; count < 12; count++) { System.out.println(“TAMU”); if (count % 4 == 0) continue; } a. b. c. d. e. 0 3 4 12 Infinite 18. We want to write a program that will print “CS111” on 100 different lines. How can we implement this with a while loop? a. int a = 0; while (a <= 100){ System.out.println(“CS111”); a++; } b. int a = 1; while (a < 100){ System.out.println(“CS111”); a++; } c. int a = 0; while (a < 100){ System.out.println(“CS111”); } d. int a = 1; while (a < 101){ System.out.println(“CS111”); a++; } e. while { if (a = 100) break; System.out.println(“CS111”); a++; } 19. Consider the following program: for (int loop1 = 0; loop1 <= 10; loop1++) for (int loop2 = 0; loop2 <= 10; loop2++) System.out.println(“1”); How many lines of output will be printed? a. 1 b. 10 c. d. e. 11 100 121 20. What is wrong with the following code? int count = 0, score=0; while (score < 34){ count++; count *= 2; } a. the syntax of the while statement is invalid. b. the operator, *=, does not exist. c. the count variable is initialized incorrectly. d. it has an infinite loop e. nothing is wrong. 21. Infinite loops: a. are not detected by the compiler b. are always bugs in the program c. occur if the conditional cannot be false d. A and C e. all of the above 22. Any method that calls a method with a throws clause in its header must a. Catch and handle the potential exception b. Have the same throws clause c. Do nothing, the called program will take care of the throws clause d. Let the java runtime handle it as it will automatically give appropriate error messages to the user if necessary e. Either A or B 23. Class objects normally have __________ that perform useful operations on their data, but primitive variables do not. a. fields b. instances c. constants d. relationships e. None of the above -- The missing word is methods 24. What is the output of the following code? int i = 0; for(;;++i) { System.out.print(i); if(i==4) break; } a. b. c. d. e. 1234 01234 0123 No output It is an infinite loop 25. For the following code, which statement is not true? public class Square { private double area; public double x; private double y; private double z; } a. x is available to code that is written outside the Square class. b. z is available to code that is written outside the Square class. c. area is not available to code written outside the Square class. d. area, x, y, and z are called members of the Square class. e. B and C 26. What will be the value of sum after the following code is executed? int sum, left = 2, middle = 3, right = 4; sum = (left++) * (middle++) / (++right); a. b. c. d. e. 0 1 2 3 None of the above 27. If a loop knows how many times it will execute, it is called a a. determinate loop b. fixed loop c. finite loop d. continue loop e. do-while loop 28. Each repetition of a loop is known as what? a. An iteration b. A cycle c. An execution d. A lap e. A circulation 29. 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); a. b. c. d. e. -1 3 -5 0 5 30. This type of loop will run of at least one iteration a. pre-test loop b. post-test loop c. sentinel loop d. selection loop e. B and C 31. What will be the value of a after the following code is executed? int a = 10, b = 20; while (b < 100) { a += b; } a. 10 b. 20 c. 50 d. 100 e. This is an infinite loop 32. 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(); } a. Either 50 or 70 b. Numbers less than 50 or greater than 70 c. Numbers in the range 50 - 69 d. Numbers in the range 50 - 70 e. The boolean condition can never be true 33. A loop that repeats a specific number of times is known as a(n) a. sentinel loop b. c. d. e. selection loop infinite loop counter-controlled loop limited loop 34. How many times will the following for loop be executed? for (int count = 20; count <= 31; count++) System.out.println("This is a print statement"); a. b. c. d. e. 10 11 12 21 Infinite 35. This is a value that signals when the end of a list of values has been reached. a. Sentinel b. Terminal c. Final d. End value e. False Boolean 36. Given the following code, what will be the output? public class Order { private int orderNum; private double orderAmount; private double orderDiscount; public Order(int orderNumber, double orderAmt, double orderDisc) { orderNum = orderNumber; orderAmount = orderAmt; orderDiscount = orderDisc; } public int getOrderTotal() { return orderAmount – (orderAmount * orderDiscount); } public int getOrderDisc() { return orderDiscount; } } public class CustomerOrder { public static void main(String[] args) { int ordNum = 1234; double ordAmount = 580.00; double discountPer = .1; Order order = new Order(ordNum, ordAmount, discountPer); double finalAmount = order.getOrderTotal(); System.out.printf("Final order amount = %.2f", finalAmount); } } a. c. c. d. e. Final order amount = $522.00 Final order amount = $580 Final order amount = $654.00 There is an error in the constructor of the class. There is an error because the object order has not been created correctly. 37. This type of loop is ideal in situations where the exact number of iterations is known. a. while b. do-while c. for d. if e. None of the above 38. Given the following statement, which statement will write "CS111" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt"); a. b. c. d. e. System.out.println(diskOut, "CS111"); DiskFile.println("CS111"); PrintWriter.println("CS111"); diskOut.println("CS111"); None of the above 39. What will be the values of x and y as a result of the following code? int x = 2, y = 8; x += y--; y = y + --x; x *= y++; a. x = 153, y = 18 b. x = 144, y = 17 c. x = 128, y = 16 d. Invalid operation e. None of the above -- -- 1. x = 2, y = 8 2. x = 10, y = 7 3. x = 9, y = 16 4. x = 144, y = 17 40. Which of the following are pre-test loops? a. while, for,if,do-while b. while, do-while c. while, for d. for, do-while e. for,if,while 41. What will be the value of x after the following code is executed? int x = 3, y = 15; while (y < 100) { x += y + 5; if (x < 50){ y += x + 5; } y += 10; } a. 103 b. 413 c. 439 d. 315 e. None of the above Start x = 3, y = 15 loop 1: x = 23, y = 53 loop 2: x = 81, y = 63 loop 3: x = 149, y = 73 loop 4: x = 227, y = 83 loop 5: x = 315, y = 93 loop 6: x = 413, y = 103 42. What will be the value of x after the following code is executed? int x = 12; do { x *= 3; } while (x < 5); a. 12 b. 36 c. 180 d. Infinite loop. e. This code will not compile. before loop: x = 12 loop 1: x = 36 after loop 2: x = 36 43. A for loop normally performs which of these steps? a. initializes a control variable to a starting value b. c. d. e. tests the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value updates the control variable during each iteration All of the above Only A and B 44. What will be printed after the following code is executed? for (int number = 3; number <= 14; number +=3) System.out.print(number + ", "); a. 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 b. 3, 6, 9, 12, 14 c. 3, 6, 9, 12, d. An infinite series of numbers e. This is an invalid for statement 45. An item that is used as a way to separate text is usually called a a. Controller b. Partition c. Doorway d. Delimiter e. Sentinel 46. Which of the following will open a file named MyFile.txt and allow you to read data from it? a. File file = new File("MyFile.txt"); b. Scanner inputFile = new Scanner("MyFile.txt"); c. File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file); d. PrintWriter inputFile = new PrintWriter("MyFile.txt"); e. File file = new File(“MyFile.txt”); Scanner inputFile = new Scanner(System.in); 47. You can use this method to determine whether a file exists. a. The Scanner class's exists method b. The File class's canOpen method c. The PrintWriter class's fileExists method d. The File class's exists method e. Java throws statement 48. Methods are commonly used to a. speed up the compilation of a program b. emphasize certain parts of the logic c. document the program d. break up variable declarations into several phases e. break a problem down into small manageable pieces 49. Which return type for a method is invalid? a. empty b. int c. double d. void e. String 50. 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.println("The winner is " + winner); } a. decideWinner (‘a’, 5.5, 4.0); b. decideWinner (“b”, 10, 6.7); c. decideWinner (‘c’, 15, 9); d. decideWinner (‘d’, 33.0, 55.0); e. All of the above TRUE / FALSE 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 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. F The PrintWriter constructor will truncate and overwrite an already existing file. T Methods are commonly used to break a problem into small manageable pieces. T Java provides unary operators that let the programmer increment and decrement variables. T Values of expressions cannot be passed as arguments to a method. F A variable’s scope is always the class in which the variable is declared. F When you open a file with the Scanner class, the class can potentially throw an IOException. T Methods can access variables that are declared in another class, as long as they are public. T A value-returning method cannot return a reference to a non-primitive type. F An object can store data. T The while loop will always execute for one iteration. F The do-while loop requires its condition to be true for the first iteration. F In a for statement, the control variable does not have to be incremented by 1 for each iteration. T In a while loop, the control variable cannot be tested against a constant value. F One class can have multiple constructor methods. T