Вы находитесь на странице: 1из 9

Advanced Programming Takehome Exam 1- Answered

Q1. Which of the following will not throw a NullPointerException?


String s = null;
1. if ((s!=null) & (s.length() > 0))
2. if ((s!=null) && (s.length() > 0))
3. if ((s==null) | (s.length() == 0))
4. if ((s==null) || (s.length() == 0))
Explanation: a NullPointerException will be thrown when s.length( ) is calld, because s is null and you can’t
call methods from a non-existing object. This method will not be called in answeres 2 nd 4 because we are
using the shortcut operators, which means if the first part of the condition already indicates the output,
the second part of the condition will not be checked – hence, the method will not be called.

Q2. Which of the following describes a fully encapsulated class?


1. Methods cannot be private
2. Variables cannot be private
3. The state of the object can only be modified through accessor method.
4. All variables are private.
5. All variables and all methods are private
Explanation: a fully encapsulated class is where all variables are private and can only be accessed by public
methods.

Q3. What is the access modifier for a class member variable which is to be used in the same package where
the class is defined?
1. protected
2. private
3. public
4. no access modifier
5. static
Explanation: no access modifier means the class member has gained the default/friendly/package access
level, where it I only accessed by fellow classes in the same package.

Q4. What will be the output?


String s = "hello";
String s1 = "there";
s.concat(s1);
s.toUpperCase();
s += "here";
System.out.println(s);

1. hello THERE
2. hello there
3. hello here
4. HELLO THERE here
5. HELLO here
Explanation: String values are immutable, which means calling the function will not change the original
value. The function will modify in a copy of the value and then returns the new resulting String value,
which we should have received in a variable in the statement s.concat(s1) and s.toUpperCase( ). Since we
haven’t done that, s will remain the same, until the += operator is used.

Q5. At what point will the String referenced at line 1 is available for garbage collection in this method?
a. String s1 = "abc";
b. String s2 = "bdc";
c. s1.concat(s2);
d. s1 = null;
e. s1 += s2;
f. System.out.println(s1);
1. Just Before d
2. Just Before e
3. Just Before f
4. never
Explanation: once the value released from its reference (in line d), it is available for the garbage collector
to collect anytime.

Q6. Which of the following are true?


class x {
int x;
public static void main(String args[]) {
x = 10;
System.out.println(" value of x "+x);
}
}
1. prints "value of x 10"
2. compilation error
3. Runtime Error
Explanation: you can’t refer to non-static members from static methods (main is static, can’t call x which is
not static).
Q7. From given code, which are possible values for 'a' to print "test2" in output

if (a >4)
System.out.println("test1");
else if (a >9)
System.out.println("test2");
else
System.out.println("test3");

1. less than 0
2. less than 4
3. between 4 and 9
4. greater than 9
5. none

Explanation: values greater than 9 will always be greater than 4 and will always apply to the first condition,
never reaching the second case.

Q8. What is the output (Assuming written inside main)

String s1 = new String("amit");


String s2 = s1.replace('m','i');
s1.concat("Poddar");
System.out.println(s1); //amit
System.out.println((s1+s2).charAt(5)); //i

1. Compile error
2. amitPoddar
o
3. amitPoddar
i
4. amit
i

Explanation: after line 3, s1 value is “amit” and s2 value is “aiit”. So, line 4 will print “amit” and line 5 will
print “i”.

Q9. What is the output (Assuming written inside main)

String s1 = new String("amit");


System.out.println(s1.replace('m','r')); //arit
System.out.println(s1); //amit
String s3="arit";
String s4="arit";
String s2 = s1.replace('m','r');
System.out.println(s2==s3); //false
System.out.println(s3==s4); //true
1. arit
amit
false
true
2. arit
arit
false
true
3. amit
amit
false
true
4. arit
amit
true
true

Explanation: remember that “==” in objects comparison will compare memory addresses not contents.

Q10. What all gets printed when the following gets compiled and run. Select the two correct answers.

public class test {


public static void main(String args[]) {
String s1 = “abc”;
String s2 = “abc”;
if(s1 == s2)
System.out.println(1); //printed
else
System.out.println(2);
if(s1.equals(s2))
System.out.println(3); //printed
else
System.out.println(4);
}
}
1. 1
2. 2
3. 3
4. 4

Explanation: both references s1 and s2 are pointing to the same object. Hence, they have the same
memory address and contents which will result in both tests of equality to be true.
Q11. Which of the following are legal array declarations? Select the three correct answers.
1. int i[5][];
2. int i[][];
3. int []i[];
4. int i[5][5];
5. int[][] a;

Q12. How can you ensure that the memory allocated by an object is freed? Select the one correct answer.
1. By invoking the free method on the object.
2. By calling system.gc() method.
3. By setting all references to the object to new values (say null).
4. Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory
used by an object.

Explanation: Garbage collection is an automatic method that works periodically at random times while the
application is executing. You can never guarantee when the memory will be free, you just help it take
decisions by setting references to null to let it know that you need this memory space no more.

Q13. At what stage in the following method does the object initially referenced by s becomes available for
garbage collection. Select the one correct answer.

void method X() {


String r = new String(“abc”);
String s = new String(“abc”);
r = r+”1”; //1
r = null; //2
s = s + r; //3
} //4

1. Before statement labeled 1


2. Before statement labeled 2
3. Before statement labeled 3
4. Before statement labeled 4
5. Never.

Q14. Name the keyword that makes a variable belong to a class, rather than being defined for each
instance of the class. Select the one correct answer.
1. static
2. final
3. abstract
4. native
5. volatile
6. transient
Q15. Which of the following are valid constructors within a class Test. Select the two correct answers.
1. test() { }
2. Test() { }
3. void Test() { }
4. private final Test() { }
5. abstract Test() { }
6. Test(Test t) { }
7. Test(void) { }

Q16. What is the result of compiling and running the following class. Select the one correct answer.
class Test {
public void methodA(int i) {
System.out.println(i);
}
public int methodA(int i) {
System.out.println(i+1);
return i+1;
}

public static void main(String args[]) {


Test X = new Test();
X.methodA(5);
}
}
Select the one correct answer.
1. The program compiles and runs printing 5.
2. The program compiles and runs printing 6.
3. The program gives runtime exception because it does not find the method Test.methodA(int)
4. The program gives compilation error because methodA is defined twice in class Test.

Explanation: when you are overloading a method, the different versions of the method should differ in
their arguments – whether by types or number.

Q17. Which of the following are true? Select the three correct answers.
1. A static method may be invoked before even a single instance of the class is constructed.
2. A static method cannot access non-static methods of the class.
3. Abstract modifier can appear before a class or a method but not before a variable.
4. final modifier can appear before a class or a variable but not before a method.
5. Synchronized modifier may appear before a method or a variable but not before a class.
Q18. Explain why it is considered good practice to limit the scope of fields and methods in object oriented
programming.

Answer:

As a general rule, developers should limit the scope of variables as much as possible. Keeping the notion
of limiting scope in every way possible, from the big things (never, ever, ever use global variables) to the
subtle little things a language can do (declare const parameters whenever possible) all can contribute to
clean, easy to test code.

Limiting scope has all kind of benefits. First, limiting scope of a variable reduces the number of places that
a given variable can be modified. Limiting scope is also, by definition, a main feature of object oriented
programming. Encapsulation is probably the first thing you know of OOP, and its basic purpose – it’s whole
reason for existing, really – is to formalize the process of limiting scope. Limiting scope also limits the
ability for people to depend on code that they shouldn’t be depending on.

More explanation can be found on: http://www.nickhodges.com/post/Limiting-Scope.aspx

Q19. Explain the following terms: i) Object; ii) Encapsulation; iii) Fields; iv) Method; v) Message

Answer:

1- Software objects are conceptually similar to real-world objects: they consist of state and related
behavior. An object stores its state in fields (variables in some programming languages) and exposes
its behavior through methods (functions in some programming languages). Methods operate on an
object's internal state and serve as the primary mechanism for object-to-object communication.
2- Encapsulation: first feature of OOP, which is concerned of limiting scope to an object’s variables by
encapsulating them inside the objects methods, which are the only way to access the variables to read
or write them.
3- Fields: are variables to store an object’s state.
4- Method: a group of instructions that express a certain behaviour, changing the status of an object.
5- Message: the main mechanism of object to object communication, which is simply calling an object’s
method to accomplish a certain behaviour.

Q20. Consider the following class definition:

public class date {

private int day; // from 1 to 31

private int month; // from 1 to 12

private int year; // from 2000 upwards

public void advance(); // move to next day

};

a) Implement a constructor that initialises new objects of date class to be set to the 1st of January 2000.

b) Implement setters for day, month and year.


c) Implement the advance method, which moves to the next day, ensuring that all data members are
updated appropriately.

Answer:

a) Constructor

public date( ) {

day = 1;

month = 1;

year = 2000;

b) setters

public void setDay( int d ) {

day = d;

public void setMonth( int m ) {

month = m;

public void setYear( int y ) {

year = y;

c) advance method

public void advance( ) {

day++;

switch (month) {

case 1,3,5,7,8,10,12: if (day>31) {

month++; day = 1;

}
break;

case 4,6,9,11: if (day>30) {

month++; day=1;

break;

case 2: if ( (year%4 == 0) && (day>29) ) {

month++; day=1;

} else if ( (year%4 != 0) && (day>28) ) {

Month++; day=1;

if (month>12) {

month = 1;

year++;

Вам также может понравиться