Академический Документы
Профессиональный Документы
Культура Документы
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.
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.
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.
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”.
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.
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.
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;
}
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.
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.
};
a) Implement a constructor that initialises new objects of date class to be set to the 1st of January 2000.
Answer:
a) Constructor
public date( ) {
day = 1;
month = 1;
year = 2000;
b) setters
day = d;
month = m;
year = y;
c) advance method
day++;
switch (month) {
month++; day = 1;
}
break;
month++; day=1;
break;
month++; day=1;
Month++; day=1;
if (month>12) {
month = 1;
year++;