APCS Final Semester 1 Test Review
When a Java string does not have surrounding double quotes, what type of error occurs?
Compile-time error
Consider the following declarations in a client class. You may assume that ClassOne and ClassTwo have default constructors. ClassOne c1 = new ClassOne(); ClassTwo c2 = new ClassTwo(); Which of the following method calls will cause an error? I. c1.methodTwo(); II. c2.methodTwo(); III. c2.methodOne(); A.None B.III only C.I only D.II only EI and II only
I only
From Reading Quiz 10: Question 4: Consider the following declarations in a client class. You may assume that ClassOne and ClassTwo have default constructors. ClassOne c1 = new ClassOne(); ClassTwo c2 = new ClassTwo(); Which of the following method calls will cause an error? I. c1.methodTwo(); II. c2.methodTwo(); III. c2.methodOne(); A.None B.III only C.I only D.II only E.I and II only
I only
What is the output of the following code snippet? public static void main(String[ ] args) { double income = 45000; double cutoff = 55000; double min_income = 30000; if(min_income > income) { System.out.println("Minimum income requirements not met."); } if(cutoff < income) { System.out.println("Income requirement is met."); } else { System.out.println("Maximum income limit is exceeded."); } }
Maximum income limit is exceeded.
In the assignment statement below, what is the purpose of the (double) ? int x = 3, y = 2; double z = x / (double)y ; A.To make sure that the result of the division is a decimal number. B.To round the answer to the nearest tenth. C.To round the answer to the nearest integer. D.To double the amount of the answer, like multiplying by 2.
To make sure that the result of the division is a decimal number.
What is the output of the code below?: public class testQuestion { public static void main (String[] args) { String sentence; String str1, str2, str3; int length1, length2; sentence = "Today is Wednesday."; str1 = sentence.substring(9, 18); str2 = str1.substring(0, 3); str3 = sentence.replace("d", "*"); length1 = sentence.length(); length2 = str1.length(); System.out.print(str3); } } A.Today is Wednesday B.To/ay is We/nes/ay. C.//d// // //d///d// D.Today / Wednesday
To/ay is We/nes/ay.
From Reading Quiz 10: Question 10: Consider the following code segment. int x = 7; double y = 3; if ((x < 10) && (y < 0)) System.out.println("Value is: " + x * y); else System.out.println("Value is: " + x / y); What is printed as a result of executing the code segment? A.Value is: 2.3333333 B.Value is: 1 C.Value is: 2 D.Value is: 21 E.Value is: 0
Value is: 2.3333333
From Reading Quiz 10: Question 7: Which of the following correctly initializes an array arr to contain four elements each with value 0? I. int[] arr = {0, 0, 0, 0}; II int[] arr = new int[3]; III int[] arr = new int [4]; for (int i = 0; i < arr.length; i++) arr[i] = 0; A. II and III only B.I and III only C.III only D.I, II, and III E.I only
| and III only
From Reading Quiz 10: Question 5: Which of the following will execute without throwing an exception? I. String s = null; String t = ""; if (s.equals(t)) System.out.println("empty strings?"); II. String s = "holy"; String t = "moly"; if (s.equals(t)) System.out.println("holy moly!"); III. String s = "holy" String t = s.substring(5); System.out.println(s + t); A. III only B.II and III only C.I only D.I and II only E.II only
|I only
From Reading Quiz 10: Question 2: Which represents correct /* implementation code */ for the Rectangle constructor? I super(labels, topLeft, botRight); II super(labels); this.myTopLeft = topLeft; this.myBotRight = botRight; III super(labels); myTopLeft = topLeft; myBotRight = botRight; A.III only B.II only C.I and II only D.II and III only
II and III only
What is the computer component that stores program instructions during a program execution?
Memory
The statement System.out.print("No" + 2 + 3 + "way") produces the output:
No23way
The statement System.out.print ("No" + (2 * 3 + "way")); produces the output:
No6way
Consider the following method. public ArrayList<Integer> mystery(int n) { ArrayList<Integer> seq = new ArrayList<Integer>(); for (int k = 0; k <= n; k++) seq.add(new Integer(k * k + 3)); return seq; } Which of the following is printed as a result of executing the following statement? System.out.println(mystery(6)); A.[4, 7, 12, 19, 28, 39] B.[3, 4, 7, 12, 19, 28, 39] C.[3, 4, 7, 12, 19, 28] D.[39, 28, 19, 12, 7, 4] E.[39, 28, 19, 12, 7, 4, 3]
[3, 4, 7, 12, 19, 28, 39]
In a main method definition, the symbols appearing inside the ( ) are called the:
parameter list
public int classSize = 36; classSize refers to ______________________.
the name of the variable
Read the following excerpt from the Syllabus to answer the following question: Because the class relies on doing your homework before class, you can't really succeed by staying up all night every Sunday. Instead, you need to budget your time. Spend at least 30 minutes every day, reading the material, and 30 minutes in the computer lab, or on your computer at home, working on problems. For most students, the best way to learn to program is to actually write programs. Spend time writing and running small "demo" programs to explore the behavior of what you are learning. The homework readings and programming assignments are outlined in the schedule. Feel free to work ahead. Since the schedule clearly outlines the homework due dates, there is no reason to turn in 'late work'. The best way to learn Java is to: A.Spend at least 30 minutes in the computer lab working on problems. B.Actually write programs using the information we have learned. C.All of these. D.Spend at least 30 minutes every day reading the material in the textbook.
ALL OF THESE!!!!
What does the following code fragment print to the console? System.out.println("C:\\Windows\\my.ini\\");
C:\Windows\my.ini\
Which of these words that can appear in a Java program are not a reserved word? *Hint: It does not show up in blue text in Dr. Java. A. int B.out C.void D.class
out
Write the method main heading: It must be exactly as Java needs it to be for your code to compile.
public static void main (String[ ] args)
Write the main method heading: It must be exactly as Java needs it to be for your code to compile.
public static void main(String[] args)
When defining a function, (unlike a procedure), you must specify a ____________.
return type
If your java source code program compiles, but displays an error message when you run it, then it must have a:
runtime error
Consider the following code segment: if (n != 0 && x / n > 100) statement1; else statement2; If n is of type int and has a value of 0 when the segment is executed, what will happen? A.statement2, but not statement1, will execute. B.statement1, but not statement2, will execute. C.A syntax error will occur. D.An ArithmeticException will be thrown.
statement2, but not statement1, will execute.
A programmer is designing a program to catalog all books in a library. She plans to have a Book class that stores features of each book: author, title, isOnShelf, and so on, with operations like getAuthor, getTitle, getShelfInfo, and setShelfInfo. Another class, LibraryList, will store an array of Book objects. The LibraryList class will include operations such as listAllBooks, addBook, removeBook, and searchForBook. The programmer plans to implement and test the Book class first, before implementing the LibraryList class. The programmer's plan to write the Book class first is not an example of
top-down development.
In a variable definition, the kind of value stored inside variable name is called its:
type
Which of the following statements correctly creates a Scanner object for keyboard input? A.Keyboard scanner = new Keyboard(System.in); B.Scanner keyboard = new Scanner(System.in); C.Scanner kbd = new Scanner(System.Keyboard); D.Scanner keyboard(System.in);
Scanner keyboard = new Scanner(System.in);
The following line of code is going to print out what type of value? System.out.print("575");
String
Which of the following code fragments will cause an error? String greeting = "Hello, World!"; A.int luckyNumber = 5; B.System.out.println(luckyNumber); C.String greeting = "Hello, Dave!"; D.System.out.println(Hi + "5");
System.out.println(Hi + "5");
From Reading Quiz 10: Question 3: Consider an ArrayList<Quadrilateral> quadList whose elements are of type Rectangle, Parallelogram, or Square. What is the effect of executing this method? A.A NullPointerException will be thrown. B.The area of each Quadrilateral in quadList will not be printed. C.The appropriate area method for each quad in quadList will be determined. D.A ClassCastException will be thrown. E.A compile-time error will occur, stating that there is no getLabels method in classes Rectangle, Parallelogram, or Square.
The appropriate area method for each quad in quadList will be determined.
What is the syntax error in the following method definition? public static area(double r) { double a; a = 3.14 x r x r; return r x r; } A.The method does not specify a return type. B.The variable a is set but never used. C.The method does not return a value a. D.The value that is returned does not match the specific variable a.
The method does not specify a return type.
From Reading Quiz 10: Question 1: Which statement about the Quadrilateral class is true? A.The method getLabels is wrong and should be getLabels(). B.The constructor for myLabels is wrong, because it should be this.myLabels = labels;. C.The Quadrilateral class inherits the methods from the Point class. D.Any subclasses of the Quadrilateral class must provide implementation code for the perimeter and area methods. E.The Quadrilateral class has a main method.
The method getLabels is wrong and should be getLabels().
True or False: All of the following statements are true. 12 % 5 is 2 12 / 5 is 2 The names int and double describe the type of variable being initialized or declared.
True
What is the output of the following Java code? int x = 0; if(x < 0) System.out.print("One "); System.out.print("Two "); System.out.print("Three");
Two Three
Which of these is a legal (that is, syntactically correct) Java class name? A.2_U B.U-2 C.U2 D.2U
U2
Text, like everything else in your computer, is stored as raw binary numbers. To treat those numbers as characters, they must be encoded. The encoding scheme used in Java is called:
Unicode
Consider the following code segment. int x = 7; double y = 3; if ((x < 10) && (y < 0)) System.out.println("Value is: " + x * y); else System.out.println("Value is: " + x / y); What is printed as a result of executing the code segment? A. Value is: 2.3333333 B.Value is: 1 C.Value is: 2 D.Value is: 21
Value is: 2.3333333
From Reading Quiz 10: Question 9: Consider the following method. public ArrayList<Integer> mystery(int n) { ArrayList<Integer> seq = new ArrayList<Integer>(); for (int k = 0; k <= n; k++) seq.add(new Integer(k * k + 3)); return seq; } Which of the following is printed as a result of executing the following statement? System.out.println(mystery(6)); A.[4, 7, 12, 19, 28, 39] B.[3, 4, 7, 12, 19, 28, 39] C.[3, 4, 7, 12, 19, 28] D.[39, 28, 19, 12, 7, 4] E[39, 28, 19, 12, 7, 4, 3]
[3, 4, 7, 12, 19, 28, 39]
Given two strings, a and b, return the result of putting them together in the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi". EXAMPLES: makeAbba("Hi", "Bye") → "HiByeByeHi" makeAbba("Yo", "Alice") → "YoAliceAliceYo" makeAbba("What", "Up") → "WhatUpUpWhat" public String makeAbba(String a, String b) { String result = ""; result = _________________ return result; } What would result need to be in order to finish this function?
a + b + b +a;
If a, b, and c are integers, which of the following conditions is sufficient to guarantee that the expression a < c || a < b && !(a == c) evaluates to true
a < c
When you try to convert String data to numeric values, using the Integer.parseInt() or Double.parseDouble() functions on a String that is improperly formatted (containing spaces, commas, or other illegal characters) you will get: A.an incorrectly converted number B.a NumberFormatException C.an InputMismatchException D.an ArithmeticException
a NumberFormatException
Each memory cell has a unique location in main memory, called the _________.
address
Programs that are designed to "work for you", carrying out a task you specify, are known as:
application programs
What does the following line of code print out to the console? System.out.println("\"\\ / \\\\ // \\\\\\ ///\""); 1.\\\\\\ /// \\\\ // \\ / 2.\ / \\ // \\\ /// 3."\ / \\ // \\\ ///" 4.// \ //// \ ////// \" 5.\\ / \\\\ // \\\\\\ ///
"\ / \\ // \\\ ///"
The elements inside the class body are surrounded with, or delimited by:
braces{ }
A programmer plans to write a program that simulates a small bingo game (no more than six players). Each player will have a bingo card with 20 numbers from 0 to 90 (no duplicates). Someone will call out numbers one at a time, and each player will cross out a number on his card as it is called. The first player with all the numbers crossed out is the winner. In the simulation, as the game is in progress, each player's card is displayed on the screen. The programmer envisions a short driver class (sometimes called test class) whose main method has just two statements: BingoGame b = new BingoGame( ); b.playBingo( ); The BingoGame class will have several objects: a Display, a Caller, and a PlayerGroup. The PlayerGroup will have a list of Players, and each Player will have a BingoCard. The relationship between the PlayerGroup and Player classes is an example of
composition
A programmer plans to write a program that simulates a small bingo game (no more than six players). Each player will call out numbers one at a time, and each player will cross out a number on his card at it is called. The first player with all the numbers crossed out is the winner. In the simulation, as the game is in progress, each player's card is displayed on the screen. The programmer envisions a short driver class whose main method has just two statements: BingoGame b = new BingoGame(); b.playBingo(); The BingoGame class will have several objects: a display, a Caller, and a PlayerGroup. The PlayerGroup will have a list of Players, and each Player will have a BingoCard. The relationship between the PlayerGroup and Player classes is an example of
composition.
To join two String objects together, use the _____________________ operator.
concatenation
If you use the String searching functions to search for a particular character or substring, and the value you're searching for is not found, the methods will return:
-1
When you define a procedure, create a ______________ for each value you want to pass into the procedure.
formal parameter
Which of the following will correctly check to see if the variable x is equal to 5?
if (x == 5)
Look at the following use of the substring() method: String word = "Hello World!"; String subs = word.substring(1, 2); The String subs will contain:
"e"
Refer to the following code fragment: double answer = 13 / 5; System.out.print("13 / 5 = " + answer); The output is 13 / 5 = 2.0 The programmer intends the output to be 13 / 5 = 2.6 Which of the following replacements for the first line of code will not fix the problem? A.double answer = 13 / 5.0; B.double answer = 13.0 / 5; C.int answer = (13 / 5); D.double answer = 13.0 / 5.0;
int answer = (13 / 5);
In the box below, write in a primitive data type.
int or Int or double or integer or Integer or Double or
Which one of the following is a correct method of declaring and initializing an integer variable with name value? value; A.int value = 30; B.value = 30; C.double crossett = 30;
int value = 30;
What is the output of the following code snippet? public static void main(String[ ] args) { int num = 100; if(num != 100) { System.out.println("Not 100"); } else { System.out.println("100"); } }
100
What value is stored in result if: int result = 13 - 3 % 3;
13
Evaluate the following expression. Make sure that your answer is correct type. For instance, if the answer is 2 and the type is int, then you should enter 2. If the answer is 2, and the type is double, then enter 2.0. 13 + Math.abs(-7) - Math.pow(2.0, 3) + 5
17.0
What is the result of the code? public static void main(String[ ] args) { int s1 = 20; if(s1 < 20) { System.out.print("1"); } if(s1 <= 40) { System.out.print("2"); } if(s1 > 20) { System.out.print("3"); } }
2
Which of the following will execute without throwing an exception? I. String s = null; String t = ""; if (s.equals(t)) System.out.println("empty strings?"); II. String s = "holy"; String t = "moly"; if (s.equals(t)) System.out.println("holy moly!"); III. String s = "holy" String t = s.substring(5); System.out.println(s + t); A.III only B.II and III only C.I only D.I and II only EII only
2 Only
What is the output of the following code snippet, if the input is 25? public static void main(String[ ] args) { Scanner cin = new Scanner(System.in); System.out.print("Please enter a number: "); int i = cin.nextInt( ); if(i > 24) { i++; } else { i--; } System.out.println(i); }
26
Refer to the following method. public static int mystery(int n) { if (n == 1) return 3; else return 3 * mystery(n - 1); } What value does mystery(1) return?
3
What is the value of the price variable after the following code snippet is executed? int price = 42; if(price < 45) { price = price + 10; } if(price < 30) { price = price * 2; } if(price < 100) price = price - 20; }
32
What is the result of the following code snippet? public static void main(String[ ] args) { double bottles = 2.0; double bottles_volume = bottles * 2; System.out.println(bottles_volume); }
4.0
Consider the following method. public static int mystery(int[] arr) { int x = 0; for (int k = 0; k < arr.length; k = k + 3) x = x + arr[k]; return x; } Assume that the array nums has been declared and initialized as follows. int[] nums = {3, 6, 1, 0, 4, 1, 2}; What value will be returned as a result of the call mystery(nums) ? A.5 B.10 C.17 D.6 E.7
5
From Reading Quiz 10: Question 8: Consider the following method. public static int mystery(int[] arr) { int x = 0; for (int k = 0; k < arr.length; k = k + 3) x = x + arr[k]; return x; } Assume that the array nums has been declared and initialized as follows. int[] nums = {3, 6, 1, 0, 4, 1, 2}; What value will be returned as a result of the call mystery(nums) ? A.5 B.10 C.17 D.6 E.7
5
Evaluate the following expression. Make sure that your answer is correct type. For instance, if the answer is 2 and the type is int, then you should enter 2. If the answer is 2, and the type is double, then enter 2.0. (12 + 3) / 4 * 2
6
Evaluate the following expression. Make sure that your answer is correct type. For instance, if the answer is 2 and the type is int, then you should enter 2. If the answer is 2, and the type is double, then enter 2.0. 26 % 10 % 4 * 3
6
Suppose x = 2 and y = 3. If the statement x *= y; is executed once, what is the value of x?
6
What is the output of the following code segment? int myAge = 63; int kathysAge = myAge; kathysAge = 60; System.out.print(myAge);
63
Read the following excerpt from the Syllabus to answer the following question: Homework: Everybody learns in different ways and at different speeds; in general though, if you're an average student and want to receive an average (C-B) grade, you should plan on spending about 7 hours each week outside of class in addition to the five hours of classroom time. If you have difficulty with the material, or if you want to receive an A in the course, you'll probably have to spend more time. If an average student wants to end up with an average B or C in this course, how much time should the student spend working on Java outside of class in addition to the 5 hours per week in the classroom?
7
Consider this hierarchy, in which Novel and Textbook are subclasses of Book: Book ^ ^ Novel Textbook Which of the following is a true statement about the classes shown? A.Each of the classes - Book, Novel, and Textbook - can have a method computeShelfLife, whose code in Book and Novel is identical, but different from the code in Textbook. B.The Textbook class can not have private instance variables that are in neither Book nor Novel.
Each of the classes - Book, Novel, and Textbook - can have a method computeShelfLife, whose code in Book and Novel is identical, but different from the code in Textbook.
Which of the following variable names is an example of a constant? final A. feetPerSecond B.FeetPerSecond C.FEET_PER_SECOND
FEET_PER_SECOND
True or False: The following statement, written in the interactions pane, will compile and print to the console. System.out.println("said Sally. "I've said so." See?);
False
Which of the following correctly initializes an array arr to contain four elements each with value 0? I. int[] arr = {0, 0, 0, 0}; II int[] arr = new int[3]; III int[] arr = new int [4]; for (int i = 0; i < arr.length; i++) arr[i] = 0; A. II and III only B.I and III only C.III only D.I, II, and III E.I only
I and III only
What is the output of the following Java code? int x = 0; if(x < 1) System.out.print("One "); System.out.print("Two "); System.out.print("Three");
One Two Three
When will the result be equal to 4? public static int whatIsIt(int x, int y) { int result = 0; if (x > y) result = 3; else result = 4; return result; } A.For all values of x and y B.This method will never return C.Only when x > y D.Only when x < y F.Only when x <= y
Only when x <= y
Consider the method public String mystery(String s) { String s1 = s.substring(0,1); String s2 = s.substring(1, s.length() - 1); String s3 = s.substring(s.length() - 1); if (s.length() <= 3) return s3 + s2 + s1; else return s1 + s2 + s3; } What is the output of: System.out.println(mystery("RELIVED"));
RELIVED
Consider the following method definition: public static void printTopHalf() { } The word printTopHalf : A.surrounds the parameter list B.is called the return type C.is called the method name D.is known as the access specifier E.surrounds the method body F.is needed if the method is called from main()
is called the method name
Consider the following method definition: public static void printTopHalf( ) { } The word static : A.is called the return type B.surrounds the method body C.is called the method name D.is known as the access specifier E.surrounds the parameter list F.is needed if the method is called from main()
is needed if the method is called from main()
In Java, Scanner is a class in which package?
java.util
Which of these must appear outside the body of a class? A.instance variables B.variable definitions C.import statements D.constructors
import statements
What is the name of the following Scanner object? Scanner info = new Scanner(System.in)
info
The devices that feed data and programs into computers are called ______.
input devices