Exam Questions Flashcards
Which access modifier should you use here to give access of the String field to class B but NOT to class C?
// filename: A.java
package foo;
public class A {
____ String s = “hello”;
}
// filename: B.java
package bar;
public class B extends A {
}
// filename: C.java
package bar;
public class C {}
Protected
How many different objects and reference variables are created in the following code?
public void createObjects() {
Object o1 = new Object();
Object o2 = o1;
Object o3 = new Object();
o3 = o1;
}
2 objects, 3 reference variables
What Type of Polymorphism is shown below?
public class Example {
public void doStuff() { System.out.println(“hello”); }
public void doStuff(int x) { System.out.println(x); }
}
Method Overloading
Which code snippet will allow for the following object instantiation?
Example myExample = new Example(“foo”);
public class Example {
public String name;
public Example(String s) {
this.name = s;
}
}
What is the output of the following code snippet?
String[] arr = {“foo”, “bar”, “foobar”, “baz”};
for (int i = 0; i < arr.length; i++) {
if (i > 1 && arr[i].length() > 3) {
System.out.println(arr[i]);
}
}
foobar
Identify the correct output for the following code snippet.
public class Example {
public static int countStatic = 0;
public int count = 0;
public static void main(String[] args) {
Example e1 = new Example();
Example e2 = new Example();
e1.increment();
e2.increment();
System.out.println(e1.count);
System.out.println(e2.count);
System.out.println(countStatic);
}
public void increment() {
count++;
countStatic++;
}
}
1
1
2
What is the output for the following code? Why?
String s1 = “hello”;
String s2 = “ world”;
s1.concat(s2);
System.out.println(s1);
String s3 = s1.concat(s2);
System.out.println(s3);
hello
hello world
Strings are immutable, therefore calling “concat” does not modify the string, but returns a new string.
How would you evaluate the following assertions based on the code below?
Can variable x be used at A? Why or why not?
Can variable y be used at B? Why or why not?
public class Scopes {
public void doStuff() {
int x = 1;
for (int i=0; i < 5; i++) {
int y = 1;
// A
}
// B
}
}
x CAN be used at A because the block scope is within the method scope and variables from an enclosing scope can always be accessed. y CANNOT be used at B because variables in block scope cannot be accessed outside of the block.
Identify the proper use of the interface declared in the code below.
public interface Driveable {
public void drive();
}
public class Car implements Driveable {
public void drive() {
System.out.println(“driving the car…”);
}
}
What is the output of this code when an exception is thrown at A?
public class ExceptionExample {
public void mayThrowException() {
try {
riskyCode(); // A
System.out.println(“after A”);
} catch(Exception e) {
System.err.println(“Uh oh - something went wrong!”);
} finally {
System.out.println(“finally block”);
}
System.out.println(“continuing…”);
}
}
Uh oh - something went wrong!
finally block
continuing…
What would be the best collection to use in the code below, if we want to ensure that no name appears twice when we print them out?
_____<String> names = getNames();
names.add("Bob");
names.add("Julie");
for (String name : names) {
System.out.println(name);
}
(assume "getNames()" returns the correct collection of names)</String>
Set
Identify the correct type of data structure to use in the code below.
____<String, Integer> countOfWords = getCounts();
Set<String> keys = countOfWords.keySet();
for (String k : keys) {
System.out.println(countOfWords.get(k));
}</String>
Map
Identify ALL of the Java or OOP concepts being used in the code snippet below. (select all that apply)
Generics
Polymorphism
Casting
Constructor
Wrapper Class
List<Integer> mylist = new ArrayList<>();</Integer>
Generics
Polymorphism
Constructor
Wrapper Class
Fill in the blank to complete the JUnit test below.
public class Addition {
public int methodToTest(int x, int y) {
return x + y;
}
}
public class AdditionTest {
@Test
public void testAddPositiveInts() {
int n = 2;
int m = 3;
_______________________________
}
}
Assert.assertEquals(5, methodToTest(n,m));
True/False
Checked exceptions must ALWAYS be caught in a try-catch block.
False
What is correct about checked exceptions? (select all that apply)
Checked Exceptions are the same as errors.
Checked exceptions are checked at compile time.
Checked exceptions should be handled.
Checked exceptions come from the programmer writing bad code and should be fixed by the programmer .
Checked exceptions are checked at compile time.
Checked exceptions should be handled.
T/F
A try block must have a catch block.
False
Which is true about arrays in Java?
Arrays are dynamic and can change size by adding elements.
Arrays have a fixed size and cannot grow or shrink.
Arrays are a primitive data type.
Arrays are immutable, so elements inserted into an array cannot be changed in any way.
Arrays have a fixed size and cannot grow or shrink.
Which use of the final keyword will lead to compilation errors?
public final class Example {
public final void foo() {
final int x = 4;
final int y = 3;
y++;
System.out.println(x*y);
}
}
on the variable y
Which series of commands compiles and then runs the program “Hello.java”?
javac Hello.java
java Hello
Match the variable scopes for each given variable in the code snippet below.
public class Greetings {
static String w = ‘welcome’;
String x = 'hello'; void speak() { String y = 'hi'; for (int z=0; z < 4; z++) { System.out.println(z); } } }
w Static/class scope
x Instance scope
y Method scope
z Block scope
T/F
The following code snippet is how we create a class in Java.
public void buildString(String start, String add) {
String toBuild = start;
for(int i=0; i < 100; i++) {
toBuild.concat(add);
}
System.out.println(toBuild);
}
False
T/F
You could use static variables if you want all instances of a class to have a variable with a shared value across all instances of the same class.
True
Assume that a, b, and c refer to instances of wrapper classes. Which of the following statements are correct?
a.equals(a) will always return true.
b.equals(c) may return false even if c.equals(b) returns true.
a.equals(b) returns same as a == b.
a.equals(b) returns false if they refer to instances of different classes.
a.equals(a) will always return true.
a.equals(b) returns false if they refer to instances of different classes.