Exam 1 Flashcards
Which of the following scenarios describes a principle of Data Hiding? Select all that apply
A) Declare all instance variables private and control access to them through a public method.
B) Use special libraries to encrypt the code of your class so nobody can know what instance variables are declared in it.
C) Do not declare public mutator methods if you do not want the value of your private instance variable to be modified
D) Make sure that you do not store sensitive information (for example passwords) inside your class code.
E) None of the above
A, C
What will be printed if we execute the following code snippet?
public class StatCalls {
public static void main (String[] args) {
int aSquare = squareNumber(5);
System.out.println(aSquare);
}
int squareNumber(int number) {
int square = number * number;
return square;
}
}
The program will not compile
You cannot access an instance variable from a static method defined in the same class. (T/F)
True
Here is a code for a Dog class:
public class Dog{
String name;
static String owner;
public static void main (String [] args) {
Dog [] pets = new Dog[2];
pets[0] = new Dog();
pets[0].name = “Lisa”;
pets[0].owner = “Marge”;
pets[1] = new Dog();
pets[1].name = “Bart”;
pets[1].owner = “Homer”;
for(Dog d : pets)
System.out.println(d.name + “ owned by “ + d.owner);
}
}
WHAT IS PRINTED?
Lisa owned by Homer
Bart owned by Homer
In the class rectangle we have two instance variables width and height. We write a method getArea() that computes the area of this rectangle. Should we make this method static?
No, should be non-static
Turtle t = new Turtle();
Turtle [ ] turtles = new Turtle [3];
for (int i=0; i<3; i++)
turtles [i] = t;
How many total Turtle objects?
1
What is printed if we execute the code snipped below?
String s;
s = new String(“Hello”);
String t = s;
String u = new String(s);
System.out.print(t==s + “ “);
System.out.print(u==s + “ “);
System.out.print(t==u);
True, False, False
The Turtle class has an integer instance variable age with the default value of 0.
Consider the following two static methods inside the Turtle class:
static void addYear(Turtle t){
t = new Turtle();
t.age ++;
}
public static void main (String[] args){
Turtle myrtle = new Turtle();
myrtle.age = 5;
addYear(myrtle);
System.out.println (“Happy birthday! “+ “You are now “ + myrtle.age + “ years old!”);
}
What is printed after we execute main?
Happy Birthday! You are now 5 years old!
What is printed after we execute the following code snippet?
ArrayList<Integer> t; // declared a list to store integers
t.add(4); // added 2 integers
t.add(5);
System.out.println(t.size());</Integer>
The code will not compile
After we execute the code segment below – how many objects need to be garbage-collected because there are no variables that reference them?
String [ ] names = new String[6];
names[0] = “Alex”;
names[1] = “Myra”;
names[2] = “Andrew”;
names[3] = names[0];
names[0] = names[2];
names[1] = names[2];
1 abandoned object
Consider a class Foo with the following instance variables:
private StringBuilder [] data;
private int size;
Now consider a copy constructor which does the following:
public Foo(Foo oldFoo) {
data = new StringBuilder[oldFoo.length];
for (int i = 0; i < oldFoo.size; i++)
data[i] = oldFoo.data[i];
size = oldFoo.size;
}
Does this copy constructor produce a deep or a shallow copy, or somewhere in between?
Something between deep and shallow
The algorithm hasDuplicates answers the question whether the array A has duplicates (at least two identical values).
What is the big O of this algorithm?
algorithm hasDuplicates (array A of size n)
for index from 0 to n − 2
for rest from index + 1 to n − 1 if (A[index] equals A[rest]) return true
return false
O(n^2)
What is the time complexity of the following Java method?
int C(int n) {
int sum = 0;
for (int i=1; i <= n; i++)
for (int j=0; j <= n; j++)
sum += j;
return sum;
}
O(n^2)
What is big O of the following algorithm that computes the sum of the first n integers?
algorithm Sum (n) {
sum: = 0
for i:=1 to n:
sum:= sum + i
return sum
O(n)
What is the time complexity of the following Java method?
static int F(int n) {
if (n == 1) return 1;
for (int i = 0; i<n; i++){
for(int j= 0; j<n; j++) {
System.out.println(“*”);
break;
}
}
return 0;
}
O(n)
Consider the following code snippet:
static void changeName(String name){
name = “Mr. “ + name;
}
public static void main (String[] args){
String myname = “Smith”;
changeName (myname);
System.out.println (myname);
}
What is printed when I run main?
Smith
Here is a variation of the previous method:
static void changeJunior (StringBuilder name){
name.append(“ Jr.”);
}
public static void main (String[] args){
StringBuilder myname = new StringBuilder (“Smith”);
changeJunior (myname);
System.out.println (myname);
}
Smith Jr.
Consider these two classes:
class Department {
String name;
public Department(String name){
this.name = name;
}
}
class Student {
Department dept;
Student (Department d){
this.dept = d;
}
Student (Student s){
this.dept = s.dept;
}
}
What is printed when we run the following code fragment?
Student s1 = new Student (new Department(“CS”));
Student s2 = new Student (s1);
s2.dept.name = “English”;
System.out.println(s1.dept.name + “ “ + s2.dept.name);
English English
Which of the following is True?
A) You can’t make an object of an Abstract class but you can of an Interface.
B) You can’t make an object of an Interface but you can of an Abstract class.
C) You must implement all the abstract methods of the Interface but you do not have to implement all the abstract methods of an Abstract class.
D) You can have both abstract and concrete methods in both Interface and Abstract class.
E) You can have static methods in both Interface and Abstract class.
F) None of the above
C, E
Consider Java classes Foo and SubFoo, where SubFoo is a subclass of Foo.
Which of the following Java statements are LEGAL?
[Note: You must indicate ALL legal statements to get credit for this problem]
A) Foo F = new Foo();
B) SubFoo S = new SubFoo();
C) Foo F = new SubFoo();
D) SubFoo S = new Foo();
A, B, C
Consider the following class definitions:
class Polymorphism {
void method1 () {
System.out.println(“Polymorphism”);
}
}
class Concept extends Polymorphism {
void method2 () {
System.out.println(“Concept”);
}
}
class Generics extends Polymorphism {
void method1() {
System.out.println(“Generics”);
}
}
We run the following program in main:
public static void main ( …) {
Polymorphism [ ] p = new Polymorphism [3];
p[0] = new Polymorphism();
p[1] = new Concept();
p[2] = new Generics();
for (int i =0; i < 3; i++)
p[i].method1();
}
}
WHAT IS PRINTED?
Polymorphism
Polymorphism
Generics
Here is an implementation of a generic method that returns the smallest of the two numbers passed as parameters:
public static <T> T min (T x, T y) {
if (x.compareTo(y) < 0)
return x;
else
return y;
}</T>
What will be printed if we run the following code in the main:
String s1 = “A”;
String s2 = “B”;
String smaller = min(s1, s2);
System.out.println(smaller);
The program will not compile
Which of the following will sort Dogs in reverse order of their height (from the tallest to the shortest)?
A) Collections.sort(dogs, new Comparator<Dog>() {
public int compare(Dog d1, Dog d2) {
return d2.height – d1.height;
}
});
B) Collections.sort(dogs, new Comparator<Dog>() {
public int compare(Dog d1, Dog d2) {
return - d1.compareTo(d2);
}
});
C) Collections.sort(dogs, new Comparator<Dog>() {
public int compare(Dog d1, Dog d2) {
return d2.compareTo(d1);
}
});
D) None of the above
E) All of the above</Dog></Dog></Dog>
E
Consider the following declarations:
public interface Ability {
public void able();
}
public class Foo implements Ability {
public void fooMethod() { // code not shown }
public void able() { // code not shown }
}
public class SubFoo extends Foo {
public void subFooMethod() { // code not shown }
}
Which of the following code segments are LEGAL? Indicate ALL LEGAL code segments. Assume default constructors exist for classes Foo and SubFoo.
A) SubFoo S = new SubFoo();
S.able();
S.fooMethod();
B) Ability A = new SubFoo();
A.able();
C) Ability A = new Ability();
A.able();
D) Foo F = new SubFoo();
F.able();
E) Ability A = new SubFoo();
A.subFooMethod();
A, B, D