0% 0 votes, 0 avg 28 1234567891011121314151617181920 This quiz randomly generates 20 questions as asked in AP Computer Science A (AP CSA) - Programming in Java. Congratulations! AP Computer Science A (AP CSA) - Programming in Java This quiz randomly generates 20 questions (in 30 mins) as asked in AP Computer Science A (AP CSA) - Programming in Java. 1 / 20 What is the output? vacation work Error No output v is initialized to false: The first if (v == true) condition is never met, so "vacation" is not printed at that stage. day is a random integer: Math.random() generates a value between 0.0 (inclusive) and 1.0 (exclusive). 6.9 * Math.random() results in a number between 0.0 and 6.9. Casting this to an int makes day a random integer value between 0 and 6, inclusive. The else if condition: day < 5 checks if the random integer is 0, 1, 2, 3, or 4. If the condition is true (the random number is 0 through 4), it will print "work" 2 / 20 Assume Dog is a subclass of Animal. Both classes have a method makeSound(). If the following code is executed, which method is called? Animal myPet = new Dog(); myPet.makeSound(); The makeSound() method in Animal. The makeSound() method in Dog. Both methods are called. Neither; this results in a compilation error. B: Java uses dynamic binding. Since the actual object is a Dog, it uses the Dog version of the method at runtime. 3 / 20 What is the minimum number of bits you would need to encode the 26 letters of the alphabet plus a space - a total of 27 characters? 2 bits 3 bits 5 bits 6 bits 4 / 20 In Java an attempt to divide an integer by 0 will result in which of the following exceptions? Stack overflow Exception Arithmetic Exception Infinite Loop Exception Type Exception In Java, an attempt to divide an integer by 0 will result in an ArithmeticException 5 / 20 What value is assigned to the variable “z” in the code below? int x = 4; int y = 1 + x / 2; int z = 0; z += (y*4); 12 8 10 An error will occur Value of y becomes 3, and it get multiplied by 4 resulting integer 12 6 / 20 Which two modifications, when made independently, enable the code to print joe:true:100.0 ? ( Choose Two) Replace line2 with: e.name = "Joe"; e.contract = true; e.salary = 100; Replace line2 with: this.name = "Joe"; this.contract = true; this.salary = 100; Replace line n1 with: this.name = new String("Joe"); this.contract = new Boolean(true); this.salary = new Double(100); Replace line n1 with: name = "Joe"; contract = TRUE; salary = 100.0f; Replace line n1 with: this("Joe", true, 100); Option B : will not work because this. cannot be used in static content / main method Option D: // --> TRUE gives complitaion error Option E: // --> could be used to call a constructor w/ 3 parameters 7 / 20 What is the output ? Hello Log 2:2 Log 2:2 Hello Log 2:2 8 / 20 If Square is a subclass of Rectangle, and Rectangle is a subclass of Shape, which of the following declarations will cause a compile-time error? Shape s = new Square(); Rectangle r = new Square(); Square sq = new Rectangle(); Object obj = new Square(); C: You can always store a more specific object (Square) in a more general reference (Rectangle), but you cannot store a general object in a specific reference without a manual cast. 9 / 20 Which code fragment, when inserted at line 6, enables the code to print true? String str2 = str1; String str2 = new String(str1); String str2 = sb1.toString(); String str2 = "Duke"; 10 / 20 Question for (int i = 0; i <= list.size() ; i++) list.set(i, new Clown)) ; list.add(list.size(), new Clown()); Clown c = list.get(list.size()); Clown c = list.remove(list.size()); list.add(-1, new Clown ()); 11 / 20 Given the classes below, which of the following is the correct way to write the Car constructor to initialize both make and model? public Car(String mk, String md) { make = mk; model = md; } public Car(String mk, String md) { super(mk); model = md; } public Car(String mk, String md) { model = md; super(mk); } public Car(String mk, String md) { super.make = mk; model = md; } B: In Java, a subclass must call the superclass constructor using super(). This call must be the first line of the subclass constructor. 12 / 20 Which of the following is a valid way to use an enhanced for-loop to print every element in int[][] arr? for (int x : arr) { System.out.println(x); } for (int[] row : arr) { for (int val : row) { System.out.println(val); } } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } for (row[] : arr) { System.out.println(row); } B: A 2D array is an "array of arrays." You must first iterate through the 1D array rows, then through the individual elements in those rows. 13 / 20 ASCII is a character-encoding scheme that uses a numeric value to represent each character. For example, the uppercase letter "G" is represented by the decimal (base 10) value 71. A partial list of characters and their corresponding ASCII values are shown in the table below. A-65 B-66 C-67 D-68 ASCII characters can also be represented by binary numbers. According to ASCII character encoding, which of the following letters is represented by the 8-bit binary value: 0100 0010 ASCII Character: A ASCII Character: B ASCII Character: D The table does not contain the value represented by the binary number 0100 0010 14 / 20 Question c = 6, d = ?, p = 9 , t = ? c = 5, d = ?, p = ? , t = 10 c = 6, d = ?, p = ? , t = ? c = 5, d = 9, p = ? , t = 10 c = 9, d = 9, p = ? , t = ? 15 / 20 Which of the following print out the last element of the integer array? int[] array = {1, 2, 3, 4}; I) int last = array[array.length]; System.out.println(last); II) int last = array[array.length-1]; System.out.println(last); III) int last = array[-1]; System.out.println(last); I only II only III only II and III I, II, and III I) int last = array[array.length];: This code will cause an ArrayIndexOutOfBoundsException error. The length of the array is 4, but valid indices are 0, 1, 2, and 3. Attempting to access an element at index 4 (the length) is an error because it is outside the bounds of the array. II) int last = array[array.length-1];: This is the correct method. Since array indices are zero-based, the last element is always at the index one less than the total length of the array. The length is 4, so the last element is at index 3, which is array[3]. This correctly accesses the value 4. III) int last = array[-1];: This code will also cause an ArrayIndexOutOfBoundsException error in most programming languages (like Java) because array indices must be non-negative integers. Negative indices are invalid. 16 / 20 Refer to the code: Consider the implementation of a writeDeck method that is added to the Deck class. I only II only Ill only I and Ill only II and III only 17 / 20 Which of the following is true about arithmetic operations? I. An arithmetic operation that uses two integer values will evaluate to an integer value. II. It is impossible to add 1 to a variable without ever using the number 1 in a statement. III. An arithmetic operation that uses a double value will evaluate to a double value. I only II only I and II I and III I, II, and III Statement I is true because in most programming languages (like Java), an arithmetic expression consisting only of integer values will always evaluate to an integer value; the fractional part (if any) is truncated, not rounded. For example, 7 / 2 results in 3, not 3.5. Statement III is true because if any value in an arithmetic operation is a double (floating-point) value, the entire expression generally evaluates to a double value to maintain precision. Type conversion occurs so that the operation is performed with floating-point arithmetic. 18 / 20 What is the output? 400 200 200 200 200 200 Compilation fails. 19 / 20 Consider the following code segment. What will the nums list contain after execution? [40, 50, 30] [10, 40, 50] [20, 50, 30] [40, 20, 30] 20 / 20 What is the output? Nothing is printed An Exception is thrown Compile Error May 15, 2004 Your score is 0% Restart quiz