Intro to programming in java final prep
Which of the following is the characteristic of Object-Oriented Programming which describes the ability for objects of different types but the same parent type to behave differently for the same method call?
Polymorphism
Referring to the above flow chart, the fact that the program repeats itself depending upon the user's input demonstrates a __________.
loop
Referring to the above Mouse class, what is the output of the following fragment? Mouse susan = new Mouse(); while (susan.getWeight() < 2) { susan.grow(); } System.out.println(susan.getWeight());
No output. There is an error and/or the code will not terminate.
What is the output of the below fragment? int[] counts = new int[5]; int sum = 0; for (int i = 5; i > 0; i--) { counts[i] = i; sum += counts[i]; } System.out.println(sum);
No output. There is an error and/or the code will not terminate.
Given: int[][] x = { {3,2,1}, {0,1}, {2} }; What is the value of x[1][x[2][0]]?
None. This uses an invalid index and will cause an error.
If we wanted to add the shuffle method to GroupOfCards but also wanted to make sure the process for shuffling a deck was different, this would be called _____________.
Overriding
Arrays in Java use __________ as indexes.
integers
"Stubs" are most commonly used with _____________.
Top down design
A "try" block is where you put code that is at risk of being affected by an exception.
True
All cards must be a member of a group of cards.
True
All primitive type names begin with a lowercase letter, and it's the convention to begin all reference type names with an uppercase letter.
True
Cards can be added to the deck.
True
Identifiers must consist entirely of letters, digits, dollar signs, and/or underscore characters, and the first character must not be a digit.
True
If we wanted to use an ArrayList instead of the array "String[] someStrings = new String[x];" (where x is some positive integer) we would write "ArrayList<String> someStrings = new ArrayList<>();".
True
It is a good idea to include comments for any code which would not be obvious to a typical Java programmer.
True
Pass-by-value means that if a method changes the value of a parameter then the value of the original argument in the calling code will not be affected.
True
The JVM allocates space for all class variables when the program starts.
True
The List interface guarantees that all ArrayLists and LinkedLists will have the get, set, add, remove, clear, and size methods.
True
You can use methods provided by the Java standard APIs to quickly and easily copy arrays.
True
What is the output of the following block of code? try { try { throw new IOException(); } catch (FileNotFoundException e) { System.out.println(1); } } catch (RuntimeException e) { System.out.println(2); } System.out.println(3);
Unless the exception is caught at a higher level the program will crash with no non-error output.
Which of the following operators does not produce a boolean value?
"+"
Given: double temp = 70.3; String raining = "Y"; What is the output when the following segment of code is run? if (temp >= 70 && !raining.equals("Y")) { System.out.println("Play basketball."); } else if (temp < 70 || raining.equals("Y")) { System.out.println("Go fishing."); } else { System.out.println("Play checkers."); }
"Go fishing."
What does the following code fragment print? String name = "Johnson"; System.out.println(name.charAt(4));
"s"
Which operator is used for string concatenation?
"+"
Given: public class SomeNum { private int x = 0; public int add(int y) { int z = x + y; x = z; return z; } } What is the output of the following fragment? SomeNum s = new SomeNum(); SomeNum t = s; s.add(5); t.add(2); System.out.println(s.add(1));
8
What is the file extension for a bytecode file?
.class
The file extension for a Java source-code file is ______.
.java
If an array has n elements, what are the values of the indices of the first and last elements?
0 and n-1
Referring to the above Mouse class, what is the output of the following fragment? Mouse frank = new Mouse(); frank.setPercentGrowthRate(3); frank.grow(); System.out.println(frank.getWeight());
1.03
Given: int a = 3, b = 2; double c = 5.0; Evaluate the below expression: (c + a / b) / 8 * 2
1.5
Given: public class Counter { private static int totalCount = 0; private int count = 0; public static int getTotalCount() { return totalCount; } public int getCount() { return count; } public void tick() { count++; totalCount++; } } What is the output of the following fragment? Counter c1 = new Counter(); Counter c2 = new Counter(); Counter c3 = c1; c1.tick(); c2.tick(); c3.tick(); System.out.println(c1.getCount()); System.out.println(c2.getCount()); System.out.println(c3.getCount()); System.out.println(Counter.getTotalCount());
2 1 2 3
Given: int x = 15; What is the value of x after executing the below statements? x--; x %= 5;
4
Referring to the above flow chart, assuming that the initial value of "tries" is 0 and the user does not like apples, how many times must they input "n" before the program will terminate?
4
What is the output of the below code fragment? int x = 5; int y = 8; int z; int i = 0; while (y < 20) { z = x + y; x = y; y = z; i++; } System.out.println(y * i);
42
Referring to the above class code, what is the output of the following fragment? SomeNum s = new SomeNum(1); for (int i = 1; i <= 3; i++) { s = s.add(i); } System.out.println(s.getValue());
7
Which of the following is true? OOP objects can be used to represent real world physical objects such as a pen, apple, or car. OOP objects can be used to represent non-physical real world concepts such as a piece of music, a color, or a bank account. OOP objects can be used to represent completely virtual concepts such as a set of files on a computer or a filter which detects and blocks malicious messages on a computer network.
All of the above.
Which of the following statements about exceptions are true? Exceptions are objects. Exceptions are useful for troubleshooting and debugging programs. Exceptions can often be prevented or handled, allowing the program to continue running.
All of the above.
Which of the following statements is most correct?
All unchecked exceptions descend from RuntimeException or Error.
Which of the following is not true about ArrayLists?
ArrayLists run faster and use less memory.
A computer program that converts Java source code to Java bytecode is called a __________.
Compiler
Software developers often use _____________________ as useful and popular solutions to common problems.
Design patterns
A class is a group of two or more objects.
False
A well-written program should never need to use try-catch because exceptions should never be thrown.
False
All groups of cards must either be able to be shuffled or sorted.
False
All of the pre-built Java API methods are class ("static") methods.
False
Each Java class automatically has a zero-parameter constructor that cannot be removed.
False
Given a class named "Circle" (its code is not important) and the following statement: Circle crs = new Circle[10]; The variable "crs" contains 10 constructed Circle objects.
False
Given the following method... public static void tripleNumber(int x) { x = x * 3; } ...and the following code fragment... int y = 5; tripleNumber(y); ...the value of y after running the fragment is 15.
False
Given... ArrayList<String> someStrings = new ArrayList<>(); And assuming that ArrayList is filled with data, the following for loop... for (int i = 0; i < someStrings.size(); i++) { System.out.println("String #" + i + " " + someStrings); } ...could be rewritten as... for (String s: someStrings) { System.out.println("String #" + i + " " + s); }
False
In the statement... String s = "Hello!"; ..."s" is a reference variable.
False
It is a good practice to omit (leave off) braces from "if" and "while" blocks whenever possible.
False
It is a good practice to wait until the end of development before starting to test your program.
False
Java is not "case sensitive". In other words, it doesn't matter whether characters are in lower or upper case.
False
Referring to the above class diagram, the account number can be changed from its default value.
False
Some cards cannot be displayed.
False
The List interface guarantees that ArrayLists and LinkedLists can be used interchangably with no differences in memory or speed performance.
False
This exact design also works for one player card games.
False
You should avoid overriding the toString method as much as possible.
False
If we have two non-null Circle variables known as "circle1" and "circle2" but we do not have access to the Circle class definition and we do not know how they were created, what best describes the possible outputs of the below statement? System.out.println(circle1.equals(circle2));
It can only be true if the Circle class overrides the equals method, and we would need to know how the equals method has been overridden to know for sure.
Which of the following is not a good use of interfaces?
Providing added application security by not exposing your method definitions.
Referring to the above flow chart assuming the flow chart matches the program behavior exactly and the value of "tries" is 0, what will happen if the user inputs "I don't know" when asked if they like apples?
The program will print "Go try one. I'll wait.", then print "Do you like apples? (y/n)", and then wait for a new input.
Consider the following fragment of code. String s = "Hello, World!"; String justHello = s.substring(0, 5); In this fragment, "s" is _______.
a calling object
A relationship where a type of object only ever belongs to a related object is called ____________.
a composition
Given: public class SomeNum { private int x = 0; public int add(int y) { int z = x + y; x = z; return z; } } In this example, z is _________________.
a local variable and is only usable inside of the add method
"public void setPercentGrowthRate(double percentGrowthRate)" is an example of ___________.
a mutator
What is the return type of the following method? public static int parseInt(String s)
int
A sequence of steps used to solve a problem is ___________.
an algorithm
Referring to the above flow chart, which would be the correct way to write a boolean expression in Java for the first decision symbol (apples = "y").
apples.equals("y")
Referring to the above class diagram and assuming we have a BankAccount object stored in a variable called "ba", which of the following fragments of code would increase the balance of the account by 500.0 and print the balance?
ba.deposit(500); System.out.println(ba.getBalance());
Using "camelCase", which would be the best name for a Java variable that describes a computer's price?
computerPrice
The process of removing abandoned objects from memory is _______________.
garbage collection
Which of the following methods is not inherited automatically by every object?
nextLine()
The "+" and "-" symbols are examples of ___________.
operators
Referring to the above class, which would be the best way to determine if two SomeNum variables (call them "s" and "t") reference the same SomeNum object?
s == t
Given: int x = 5; boolean e = x % 2 == 0; boolean f = e || x / 2 < 3; What is the value of f?
true
The following block of code... int i = 0; while (i < 10) { System.out.println(i); i++; } ...could be rewritten as... for (int i = 0; i < 10; i++){ System.out.println(i); }
true