CS 200: Exam 3 - Exam 1 & 2 Questions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Which XXX completes the code? import java.util.Scanner; public class GuessMyAge { public static void myGuess(int low, int high, Scanner scnr) { int mid; char userAnswer; mid = (low + high) / 2; System.out.print("Is it " + mid + "? (</>/=): "); userAnswer = scnr.next().charAt(0); if ((userAnswer != '<') && (userAnswer != '>')) { System.out.println("Great!"); } else { if (userAnswer == '>') { XXX } else { myGuess(low, mid, scnr); } } } public static void main(String[] args) { Scanner scnr = new Scanner(System.in); myGuess(0, 100, scnr); } } a.) myGuess (mid, high, scnr); b.) myGuess(mid + 1, high, scnr); c.) myGuess(low, mid, scnr); d.) myGuess (low, high, scnr );

b.) myGuess(mid + 1, high, scnr);

Which of the following may be associated with a type parameter to specify valid class types? a.) generic type b.) type bound c.) type extension d.) another type parameter

b.) type bound

What is output? public class BaggageWeight { static void baggageWeight(int weight) { boolean value = false; try { while (!value) { if (weight > 30) { throw new Exception("Double Max Weight"); } if (weight > 15) { throw new Exception("Excess Weight"); } value = true; System.out.println("Accepted"); } } catch (Exception excpt) { System.out.println(excpt.getMessage()); } } public static void main(String[] args) { baggageWeight(42); } } a.) Double Max Weight Accepted b.) Accepted c.) Double Max Weight d.) Double Max Weight Excess Weight

c.) Double Max Weight

What is output? public class BaggageWeight { static void baggageWeight(int weight) { boolean value = false; try { while (!value) { if (weight > 30) { throw new Exception("Double Max Weight"); } if (weight > 15) { throw new Exception("Excess Weight"); } value = true; System.out.println("Accepted"); } } catch (Exception excpt) { System.out.println(excpt.getMessage()); } } public static void main(String[] args) { baggageWeight(42); } } a.) Double Max Weight Excess Weight b.) Accepted c.) Double Max Weight d.) Double Max Weight Accepted

c.) Double Max Weight

What is output? public class FindPerimeter { public static int perimeter(int arr[]) { int perimeter = 0; int i; for (i = 0; i < arr.length; i++) { perimeter += arr[i]; } return perimeter; } public static void main(String[] args) { int sides[] = {1,3,4,5}; try { System.out.println("Perimeter for given sides is " + perimeter(sides)); } catch (Exception excpt) { System.out.println("Caught Exception: " + excpt.getMessage()); } finally{ System.out.println("End"); } } } a.) Perimeter for given sides is 13 b.) End c.) Perimeter for given sides is 13 End d.) Error: The catch block is missing

c.) Perimeter for given sides is 13 End

Which construct is the exception handler? try { if (val < 0) { throw new Exception("Error!"); } } catch (exceptionType excpt) { System.out.println(excpt.getMessage()); } a.) throw new Exception (...) b.) try {...} c.) catch {...} d.) exceptionType excptObj

c.) catch {...}

Which XXX will complete the code? public class RecursiveCalls { public static int convertToBinary(int num) { if (num == 0) { return 0; } else { return (num % 2 + 10 * convertToBinary(num / 2)); } } public static void main (String [] args) { System.out.print("The Binary number is: "); System.out.println(convertToBinary(7)); } } a.) convertToBinary(num); b.) convertToBinary(num * 2); c.) convertToBinary(num / 2); d.) convertToBinary();

c.) convertToBinary(num / 2);

A recursive algorithm is based on _____ application(s) of the same algorithm on smaller problems. a.) single b.) zero c.) repeated d.) infinite

c.) repeated

The correct syntax for declaring a type parameter is _____. a.) <TheType extends Compare<TheType>> b.) TheType implements Compare<TheType> c.) <TheType implements Comparable<TheType>> d.) <TheType extends Comparable<TheType>>

d.) <TheType extends Comparable<TheType>>

What is the output? public class RecursiveFactorial { public static int factorial(int factValue) { if (factValue <= 0) { return 1; } else { return factValue*factorial(factValue-1); } } public static void main (String [] args) { int userVal = -5; System.out.println("Factorial = " + factorial(userVal)); } } a.) Error b.) Factorial = -120 c.) Factorial = 120 d.) Factorial = 1

d.) Factorial = 1

Which is output when inputVal is 0? try { inputVal = scnr.nextInt(); if (inputVal < 0) { throw new Exception("Input should be positive"); } System.out.print("Input squared: " + inputVal * inputVal); } catch (Exception e) { System.out.print(e.getMessage()); } a.) Input should be positive b.) Input squared: 0 Input should be positive c.) Exception d.) Input squared: 0

d.) Input squared: 0

What is output? public class DayEnd { static void timeHour(int hours) { int timeLeft; try { if (hours > 23) { throw new Exception("Invalid Hour!"); } timeLeft = 24 - hours; System.out.println("Time Left: "+timeLeft); } catch (Exception excpt) { System.out.println("Oops"); System.out.println(excpt.getMessage()); } } public static void main(String[] args) { timeHour(24); } } a.) Oops Invalid Hour! Time Left: 0 b.) Invalid Hour! Oops c.) Invalid Hour! Time Left: 0 d.) Oops Invalid Hour!

d.) Oops Invalid Hour!

Find the output statement that is used for debugging the following code: import java.util.Scanner; public class GuessMyAge { public static void myGuess(int low, int high, Scanner scnr, String indent) { int mid; char userAnswer; mid = (low + high) / 2; System.out.print("Is it " + mid + "? (</>/=): "); userAnswer = scnr.next().charAt(0); if ((userAnswer != '<') && (userAnswer != '>')) { System.out.println("Great!"); } else { if (userAnswer == '<') { System.out.println(indent + "User is younger."); myGuess(low, mid, scnr, indent + " "); } else { myGuess(mid + 1, high, scnr, indent + " "); } } } public static void main(String[] args) { Scanner scnr = new Scanner(System.in); System.out.println("Finding your age!"); myGuess(0, 100, scnr, " "); } } a.) System.out.println("Great!"); b.) System.out.print("Is it " + mid + "? (</>/=): "); c.) System.out.println("Finding your age!"); d.) System.out.println(indent + "User is younger.");

d.) System.out.println(indent + "User is younger.");

An output statement can help debug recursive methods, especially if indented based on the _____. a.) base case b.) decision statements c.) recursive method d.) recursion depth

d.) recursion depth

Which XXX completes the definition of the generic method, avgNum? public class FindAverage { XXX { long tripleSum; tripleSum = item1.longValue() + item2.longValue() + item3.longValue(); return tripleSum / 3; } } public static <T extends Number> long avgNum(long item1, long item2, long item3) public static <T extends Number> long avgNum(TheType item1, TheType item2, TheType item3) public static <TheType extends Number> avgNum(TheType item1, TheType item2, TheType item3) public static <TheType extends Number> long avgNum(TheType item1, TheType item2, TheType item3)

public static <TheType extends Number> long avgNum(TheType item1, TheType item2, TheType item3)

Which of the following recursive methods will result in a stack overflow when main() calls fact(10)? public static int fact(int n) { if (n != 100) return 1; else return n*fact(n-1); } public static int fact(int n) { if (n == 100) return 1; else return n*fact(n-1); } public static int fact(int n){ if (n <= 100) return 1; else return n*fact(n-1); } public static int fact(int n) { if (n < 100) return 1; else return n*fact(n-1); }

public static int fact(int n) { if (n == 100) return 1; else return n*fact(n-1); }

What is output for countNum(5);? public class CountNum { public static void countNum(int num) { if (num <= 0) { System.out.println("Done"); } else { System.out.print(num + " "); countNum(num - 2); } } } a.) 5 3 1 Done b.) 5 3 1 0 Done c.) 5 4 3 2 1 Done d.) 5 3 1 -1 Done

a.) 5 3 1 Done

Which calculation does not have a natural recursive solution? a.) Percentage b.) Factorial c.) Fibonacci d.) Greatest common divisor

a.) Percentage

The _____ case defined in a function stops recursion. a.) base b.) start c.) terminal d.) loop

a.) base

A _____ describes the operation performed by the recursive algorithm. a.) base case b.) method c.) condition statement d.) reduction step

a.) base case

What is output? public class RecursionExample { public static void printer() { System.out.println("hello"); printer(); } public static void main(String[] args) { printer(); } } a.) stack overflow b.) hello c.) hello hello d.) hello hello hello

a.) stack overflow

Which Java class provides static methods that operate on lists such as ArrayList? a.) LinkedList b.) Collections c.) Comparable d.) Exceptions

b.) Collections

A stack overflow typically causes a program to _____. a.) return a value b.) crash c.) give an incorrect output d.) give multiple outcomes

b.) crash


Set pelajaran terkait

BIO CHAPTER 8 POST TEST LAST MISSED

View Set

Chapter 7: Individual and Group Decision Making How Managers Make Things Happen

View Set

Loops Test Review: What's the Output??

View Set