March 01, 2015

Which regular expression, inserted at line 12, correctly splits test into "Test A", "Test B", and "Test C"?

Given:

11. String test = "Test A. Test B. Test C.";
12. // insert code here
13. String[] result = test.split(regex);



A. String regex = "";
B. String regex = " ";
C. String regex = ".*";
D. String regex = "\\s";
E. String regex = "\\.\\s*";
F. String regex = "\\w[ \.] +";









Answer: E

Explanation:

Remember that if you need to create a String that contains a double quote " or a backslash \ you need to add an escape character first.If you need to search for periods (.) in your source data and If you just put a period in the regex expression, you get the "any character" behavior. 

So, what if you try \. ? Now the Java compiler thinks you're trying to create an escape sequence that doesn’t exist. 

The correct syntax to do so is 
String s = "ab.cde.fg";
String[] tokens = s.split("\\.");
which gives Output as :
abcdefg

Removes .(dot) and joins them.

Similarly \\s refers any occurrence of White Space and \\s* refers to occurrence of White Space character zero or more times .

String regex = "\\.\\s*"; is used which tells compiler that when both .(dot) and any number of White  Space occurs together then remove them .Therefore output will be Test ATest BTest C .



What is the result when the programmer attempts to compile the code and run it with the command java Converter 12?

Given:

11. class Converter {
12. public static void main(String[] args) {
13. Integer i = args[0];
14. int j = 12;
15. System.out.println("It is " + (j==i) + " that j==i.");
16. }
17. }



A. It is true that j==i.
B. It is false that j==i.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.









Answer: D

Explanation :

The java command-line argument is an argument i.e. passed while running the java program.The arguments passed from the console is a String type argument and can be received in the java program and it can be used as an input.

In the above example "12" is passed as a String when we run - java Converter 12 .Now we are storing String - 12 in integer i without using static method Integer.parseInt(str) which leads to Compile Time Error > 

line no:5: incompatible types
found : java.lang.String
required: java.lang.Integer
Integer i = args[0];

Solution to the above problem is ,we need to use  Integer.parseInt(arg[0]) and then Store it in Integer i .



Which three are valid on line 12 ?

Given :

(Choose three.)

11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. } 




A. final
B. static
C. native
D. public
E. private
F. abstract
G. protected









Answer: A,B,D

Explanation :

An interface by default 100% abstract ,So  while declaring any method by default compiler implicitly adds abstract keyword in the method header and while declaring any variable or constant by default compiler implicitly adds public static final .

While declaring any constant the variable name must be All Capitals .In the above question MY_VALUE is marked as final and therefore once 10 is assigned to it we can't change or alter its value again.

Therefore public static final int MY_VALUE = 10; is a correct syntax for declaring Constant and public static final are the three correct options .




Which code fragment, inserted at line 24, outputs "123abc 123abc"?

Given:

22. StringBuilder sb1 = new StringBuilder("123");
23. String s1 = "123";
24. // insert code here
25. System.out.println(sb1 + " " + s1);



A. sb1.append("abc"); s1.append("abc");
B. sb1.append("abc"); s1.concat("abc");
C. sb1.concat("abc"); s1.append("abc");
D. sb1.concat("abc"); s1.concat("abc");
E. sb1.append("abc"); s1 = s1.concat("abc");
F. sb1.concat("abc"); s1 = s1.concat("abc");
G. sb1.append("abc"); s1 = s1 + s1.concat("abc");
H. sb1.concat("abc"); s1 = s1 + s1.concat("abc");









Answer: E

Explanation:

public StringBuilder append(String s) :is used to append the Specified String with the given String. Here sb1 is a reference variable of type of StringBuilder which has 123 as String in it. 

Now we need to append 123 with abc so we call sb1.append("abc"); which gives the output 123abc and remember unlike normal String where operation on 2 strings forms new String which doesn't affect the original given String but in String Builder it does affect the original String when we are using append method, therefore 123abc is stored in sb1 itself .


Next we are declaring String s1 as 123 and concat with String abc which gives new String 123abc .Now in-order to change the contents of original String s1 we store new String 123abc in s1 by writing s1 = s1.concat("abc"); .


By SOP of sb1 and s1 seperated by blank space we get the output as 123abc 123abc.Option E.

What is Stored in y[2][1] and What is the Output of Below Code Snippet ?

Given:

1. class Alligator {
2. public static void main(String[] args) {
3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
4. int [][]y = x;
5. System.out.println(y[2][1]);
6. }
7. }



A. 2
B. 3
C. 4
D. 6
E. 7
F. Compilation fails.









Answer: E


Explanation:

In General we can declare int array as int[] x or int []x or int x[] all of them are equivalent and same for one dimensional array.For two dimensional array int [][] x or int []x[] or int x [][] all of them equivalent and same .



Here in the Line 3 we declaring an integer multi dimensional array named as x and initializes integer values 1,2,0,0 for 0th row , 3,4,5,0 for 1st row , 6,7,8,9 for 2nd row .


Next we are storing the reference variable x in the y , therefore integer reference variable x and y both point to same array with elements 1,2,0,0 for 0th row , 3,4,5,0 for 1st row , 6,7,8,9 for 2nd row .

Then we are accessing 2nd row first element - y[2][1] .Remember array index starts with 0 and not from 1.Output is :7 ,Option E.

February 27, 2015

Which two code Fragments correctly create and initialize a Static Array of int elements?

Given :

(Choose two.)

A. static final int[] a = { 100,200 };
B. Line 1:static final int[] a;
   Line 2:static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2]{ 100,200 };
D. Line 1:static final int[] a;
   Line 2:static void init() { a = new int[3]; a[0]=100; a[1]=200; }









Answer: A,B


Explanation :

In general we declare an Array in this form [Access Specifiers][optional modifiers] Datatype [] ref-var = {Initialization} or new Datatype[Size] for one Dimensional Array. 

For example :
public static String[] stringArray = new String[size];

OR

public static String[] stringArray = {"String1","String2","String3"};

Therefore Option A is correct with the Syntax.

Initialization blocks run when the class is first loaded (a static initialization block) or when an instance is created (an instance initialization block).

In Option B ,reference variable declaration and instance is created with initialization is divided 2 parts as shown in Line 1 and Line 2 in Option b .Since Static block can access Static data members and initialize the data values for integer array ,Option is Correct in Syntax.Option A,B.







What gets Printed when a.foo() & b.foo() Called ?

Given:

11. class Alpha {
12. public void foo() { System.out.print("Afoo "); }
13. }
14. public class Beta extends Alpha {
15. public void foo() { System.out.print("Bfoo "); }
16. public static void main(String[] args) {
17. Alpha a = new Beta();
18. Beta b = (Beta)a;
19. a.foo();
20. b.foo();
21. }
22. }


A. Afoo Afoo
B. Afoo Bfoo
C. Bfoo Afoo
D. Bfoo Bfoo
E. Compilation fails.
F. An exception is thrown at runtime.







Answer: D


Explanation :

Super class Alpha reference variable to pointing to subclass object beta .So due to Runtime Polymorphism or Dynamic method Dispatch the overridden method foo() of Beta Class Object is called and Not foo method Alpha Class Object.

Even reference variable 'a' is of Type Alpha ,the Overridden method foo of Beta Class is called and therefore Bfoo is Printed first.

Now the reference variable 'a' which is of type Alpha is downcasted to Beta type.Therefore Beta b variable is pointing to Beta Object ,Where Alpha a was pointing to and therefore Beta Object foo method is called which prints Bfoo Again.Option D

Output :Bfoo Bfoo With Warning :Possible Loss of Data By Downcasting.

Remember :Which method is called is decided at runtime by Type of Object and not on Reference variable.Also Compiler only checks whether a reference variable of a Class is having its method or not.

Which code, Inserted at line 16, Correctly retrieves a local instance of a Point object?

Given:

10. class Line {
11. public class Point { public int x,y;}
12. public Point getPoint() { return new Point(); }
13. }
14. class Triangle {
15. public Triangle() {
16. // insert code here
17. }
18. }




A. Point p = Line.getPoint();
B. Line.Point p = Line.getPoint();
C. Point p = (new Line()).getPoint();
D. Line.Point p = (new Line()).getPoint();







Answer: D

Explanation :

getPoint() method is declared inside Line Class and outside Point class.So in-order to call getPoint() method we must first instantiate Line Class object . Therefore (new Line()).getPoint(); .

The reference variable of inner Point class must be declared in the form OuterClass.InnerClass ,ie Line.Point p ,Since we instantiate inner class Point object in Triangle class constructor .

Output is x and y integer variables of Point object is initialized with Zero and Point Object reference is returned to Triangle Constructor .And the correct Syntax to do so is Option D .


Here are the some other ways to Instantiate Inner class objects outside Outer Class :

new MyOuter().new MyInner(); or outerObjRef.new MyInner();