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.