March 05, 2015

What is the result When f.addFive() and System.out.println(f.a) is Called ?

Given:

31. class Foo {
32. public int a = 3;
33. public void addFive() { a += 5; System.out.print("f "); }
34. }
35. class Bar extends Foo {
36. public int a = 8;
37. public void addFive() { this.a += 5; System.out.print("b " ); }
38. }

Invoked with: Foo f = new Bar(); f.addFive(); System.out.println(f.a);

What is the result?


A. b 3
B. b 8
C. b 13
D. f 3
E. f 8
F. f 13
G. Compilation fails.
H. An exception is thrown at runtime.









Answer:A

Explanation :

At run-time JVM calls the overridden method (addFive) of the Class Bar which extends Foo.Since this.a + = 5 is written in Line 37,Bar Object's integer variable a = 8 is added with 5 and will have its result 13.Then b is printed .


after executing or printing b,we have now a tricky part where f.a is written withing System.out.prinln () method.Question arises - Which value of a is printed ? Is it a value of Bar Class or a value of Foo class.

Always at Run-time JVM calls only overridden method of subclass which is decided by the Type of Object(In the above case Bar is the type of Object).But whenever we call or invoke any other data member like class's variables,It is decided by Type of Reference variable  and not by type of Object.

In the above question f.a , f is of type Foo ,now compiler first checks whether class Foo has int a variable ? -- Yes ,if it has ,Then call that Foo class int a value .Therefore a value 3 is printed along with b. (b 3) - Option A.

Which Man class properly represents the relationship "Man has a best friend who is a Dog"?

Given :

A. class Man extends Dog { }
B. class Man implements Dog { }
C. class Man { private BestFriend dog; }
D. class Man { private Dog bestFriend; }
E. class Man { private Dog<bestFriend>; }
F. class Man { private BestFriend<dog>; }









Answer: D

Explanation:

If the Class has entity reference then it is called Aggregation.Aggregation is also known for "HAS - A" relationship.
In the above question we have "Man has a best friend " which represents container-ship or "HAS-A" relationship . Therefore bestFriend is one of the Member of Man .

"Who is a Dog".This part may confuses us ,but in the question it is very much clear in stating that best friend is of type Dog . So Datatype is Dog and variable name is bestFriend .


Since we have Dog bestFriend as a data member of Man ,therefore Man has to be a Class which has this data member which satisfies "HAS-A" relationship.

Option D.


What design flaw is most likely the cause of these new bugs?

Given :

A company that makes Computer Assisted Design (CAD) software has, within its application,some utility classes that are used to perform 3D rendering tasks. The company's chief scientist has just improved the performance of one of the utility classes' key rendering algorithms, and has assigned a programmer to replace the old algorithm with the new algorithm. When the programmer begins researching the utility classes, she is happy to discover that the algorithm to
be replaced exists in only one class. The programmer reviews that class's API, and replaces the old algorithm with the new algorithm, being careful that her changes adhere strictly to the class's API. Once testing has begun, the programmer discovers that other classes that use the class she changed are no longer working properly. 



A. Inheritance
B. Tight coupling
C. Low cohesion
D. High cohesion
E. Loose coupling
F. Object immutability








Answer: B


Explanation:

Definition of coupling:Degree to which one class knows about another class.


When we are having loose coupling and tight coupling ?

Suppose there are two Classes, Class A and Class B .If the Class A depends or relies only on those parts of Class B ,which is exposed using the interface of Class B .Such Scenario between the Classes is Called as loose coupling.


Suppose there are two Classes, Class A and Class B .If the Class A depends or relies on the parts of Class B ,which are not exposed through the interface of Class B .Such Scenario between the Classes is Called as Tight coupling.


In the above question the new Algorithm is asked to be implemented on Class's API and all other Classes which depends or relies on Class's API got affected due to Tight coupling.Option B.

March 02, 2015

What is the result when test("four") ,test("tee"),test("to") is Called ?

Given:

11. public static void test(String str) {
12. int check = 4;
13. if (check = str.length()) {
14. System.out.print(str.charAt(check -= 1) +", ");
15. } else {
16. System.out.print(str.charAt(0) + ", ");
17. }
18. } 
     \\ and the invocation:
21. test("four");
22. test("tee");
23. test("to");


A. r, t, t,
B. r, e, o,
C. Compilation fails.
D. An exception is thrown at runtime.








Answer: C

Explanation:

The biggest problem Java programmers confuse is with - if(condition) where to the condition they take it as  0 or 1 ( << its Wrong ,its not 0 or 1).In Java the Condition has to be either true or false (in words) and not 0 or 1.


Java has a separate data type called Boolean for condition  which evaluates to true or false (in words).But In Languages like C/C++ it is not so ,we use 1 to represent true and 0 to represent false.

Output of the above code is 
error: incompatible types
         if(check = str.length() ) {
                     ^
required :Boolean
found :    Int


The Entire trick in the above code is that they have used assignment operator = Instead of relational operator == .Therefore leads to Compilation failure .Option C





What is the result when you run the below Java program ?

Given:

13. public class Pass {
14. public static void main(String [] args) {
15. int x = 5;
16. Pass p = new Pass();
17. p.doStuff(x);
18. System.out.print(" main x = " + x);
19. }
20.
21. void doStuff(int x) {
22. System.out.print(" doStuff x = " + x++);
23. }
24. }




A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuff x = 6 main x = 6
D. doStuff x = 5 main x = 5
E. doStuff x = 5 main x = 6
F. doStuff x = 6 main x = 5








Answer: D


Explanation:

This is a good example of how parameters are passed into the methods.Remember that always parameters are passed copy by value i.e changes in any value of the parameter in called method doesn't have affect in parameter in calling method.

Called method: doStuff()
Calling method : main method ( p.doStuff calls doStuff() method).

In the above example x is initialized with 5 and x is passed by copy by value to doStuff method .Here in the doStuff method we are post incrementing the value of x.But always SOP will print the value of x first then when ;(semicolon) is encountered the x gets incremented to 6 from 5.So doStuff x = 5 is printed.

Now the x in the main method will be 5 and not 6 because both methods x value have different memory locations and resources.  Option D.


What is the result when we try to Compile and Run ?

Given:

11. public class ItemTest {
12. private final int id;
13. public ItemTest(int id) { this.id = id; }
14. public void updateId(int newId) { id = newId; }
15. 
16. public static void main(String[] args) {
17. ItemTest fa = new ItemTest(42);
18. fa.updateId(69);
19. System.out.println(fa.id);
20. }
21. }




A. Compilation fails.
B. An exception is thrown at runtime.
C. The attribute id in the ItemTest object remains unchanged.
D. The attribute id in the ItemTest object is modified to the new value.
E. A new ItemTest object is created with the preferred value in the id attribute.









Answer: A


Explanation :

When you Compile and run the above program you get 

ItemTest.java:14:error: Cannot Assign value to final variable id 
 public void updateId(int newId) { id = newId; }
                                   ^
1 error .

Compilation fails because once we assign a value to final variable we can't change its value again .This is not because id is marked private or we haven't used this.id in updateId method .

Remember when you mark any variable as final ,Only once we can assign value to it and we can't change its value again.Burn that in.





When line 15 is reached, how many objects are eligible for the garbage collector?

Given:

3. interface Animal { void makeNoise(); }
4. class Horse implements Animal {
5. Long weight = 1200L;
6. public void makeNoise() { System.out.println("whinny"); }
7. }
8. public class Icelandic extends Horse {
9. public void makeNoise() { System.out.println("vinny"); }
10. public static void main(String[] args) {
11. Icelandic i1 = new Icelandic();
12. Icelandic i2 = new Icelandic();
13. Icelandic i3 = new Icelandic();
14. i3 = i1; i1 = i2; i2 = null; i3 = i1;
15. }
16. }



A. 0
B. 1
C. 2
D. 3
E. 4
F. 6









Answer:E

Explanation:

We use diagrammatic approach for your better understanding.Please read the question carefully before tracing the images below.

Initially when  three Icelandic are objects are created using reference variable i1,i2,i3   using the statements 
            Icelandic i1 = new Icelandic();
            Icelandic i2 = new Icelandic();
            Icelandic i3 = new Icelandic();

Remember Long weight is a primitive type of object which is within Icelandic object. i1 is pointing to Object Icelandic A with weight object of Long type .Therefore Totally 2 Objects for i1 .Similarly 2 Objects for i2 and 2 Objects for i3. And also remember that single reference variable can't point to 2 objects but here in this case Long Weight object is a primitive data member of Icelandic Object. 

After i3 = i1



After i1 = i2 



After i2 = NULL

Since i1 pointing to Icelandic B ,when we write i3 =i1  - i3 is made to point to Icelandic B .


After i3 = i1

Therefore Icelandic A along with its Long weight object and Icelandic C along with its Long weight Object is eligible for Garbage Collection .Total 4 Objects are eligible for Garbage Collection.Option E.

March 01, 2015

Which code should be inserted at line 1 of Demo.java to compile and run Demo to print "pizzapizza"?

Given:

1. // Class Repetition
2. package utils;
3. public class Repetition {
4. public static String twice(String s) { return s + s; }
5. } 

// and given another class Demo: 


1. // insert code here <<

2.
3. public class Demo {
4. public static void main(String[] args) {
5. System.out.println(twice("pizza"));
6. }
7. }





A. import utils.*;
B. static import utils.*;
C. import utils.Repetition.*;
D. static import utils.Repetition.*;
E. import utils.Repetition.twice();
F. import static utils.Repetition.twice;
G. static import utils.Repetition.twice;









Answer: F

Explanation:

Static import feature allows to access the static members of a class without the class qualification.The Correct Syntax of writing Static import is :import static fully qualified package name.

Twice method is present within Class Repetition and Class Repetition is present within utils Package So the fully qualified package will be utils.Repetition.twice.

When Twice method is called ,it will return PizzaPizza two concatenated String and will print PizzaPizza from the SOP.Therefore Option F is correct.