Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.


Please wait...

JAVA Programming :: Basic Concepts

Home > JAVA Programming > Basic Concepts > General Questions

11. What will happen when you attempt to compile and run the following code?

public class MyField{
String s;
public static void main(String argv[]){
MyField m = new MyField();
m.amethod();
}
void amethod(){
System.out.println(s);
}
}

Answer: Option D

Explanation:

The above code will compile fine, no problem in that, but the output will be null, since variable 's' has not been initialized with some value.

Hence, output will be null.

Workspace


Tags:  Virtusa

Answer: Option B

Explanation:

Debugging is the process of detecting and removing of existing and potential errors (also called as ‘bugs’) in a software code that can cause it to behave unexpectedly or crash. To prevent incorrect operation of a software or system, debugging is used to find and resolve bugs or defects.

Workspace


Tags:  Virtusa

13. Output of the below partial code.
String[] elements = {"Java","is","platform","independent"};
String result = (elements.length > 0) ? elements[0]:null;
System.out.printf(result);

Answer: Option B

Explanation:

The condition (elements.length>0) will result to True, since the length size of elements array is 4, which is greater than 0.

Therefore, elements[0] will be stored in result, or we can simply say, result = "Java".

Hence, output will be Java.

Workspace


Tags:  Virtusa

14. What will happen when you attempt to compile and run the code in main method:

for(int i=0;i<5;) {
System.out.println(i);
i++;
continue;
}

Answer: Option C

Explanation:

We can dry run the code as-

    Value of 'i'                                         Test condition(i<5)                    Output              i++                            

        0                                                            True                                        0                   i=0+1=1

        1                                                            True                                        1                   i=1+1=2

        2                                                            True                                        2                   i=2+1=3

        3                                                            True                                        3                   i=3+1=4

        4                                                            True                                        4                   i=4+1=5

        5                                                            False                                       -                          -



Workspace


Tags:  Virtusa

15. Given the following constructor what will element 1 of mycon contain?

MyCon(){
int[] mycon= new int[5];
}

Answer: Option A

Explanation:

Since, mycon is an integer array, the default value for an integer array is 0.

Hence, mycon[]={0,0,0,0,0}; will be set as default, if nothing is assigned to the array.

Therefore, correct option is A 

Workspace


Tags:  Virtusa

16. Consider the following code:

public class ManTin {
public static void main(String[] args) {
int j=2,y=3,z=9;
for(;j<6;j++) {
y = (++y + z++);
System.out.println(y+z);
}
}
}
Which of the following gives the valid output for the above code?

Answer: Option C

Explanation:

23 35 48 62

Workspace


Tags:  Virtusa

17. What exception type does the following program throw?

public class Test {
public static void main(String[] args) {
System.out.println(1/0);
}
}

Answer: Option A

Explanation:

ArithmeticException

Workspace


Tags:  Virtusa

18. What is the Output:

public class SwitchTest {
public static void main(String argv[]) {
SwitchTest ms=new SwitchTest();
ms.display();
}
public void display() {
int k=10;
switch(k){
case 10:
System.out.println("10");
case 30:
System.out.println("30");
case 20:
System.out.println("20");
default:
System.out.println("40");
}
}
}

Answer: Option A

Explanation:

10 ,30 , 20 , 40

Workspace


Tags:  Virtusa

19. Which Exception Will occur

class Test {
public static void main(String[ ] args) {
try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
System.out.println("Welcome to Java");
} catch (Exception ex) {
System.out.println(ex);
}
}
}

Answer: Option A

Explanation:

NumberFormatException

Workspace


Tags:  Virtusa

20. Which keyword when applied on a method indicates that only one thread should execute the method at a time. Select the one correct answer.

Answer: Option C

Explanation:

synchronized is the modifier applicable only for methods and blocks, but not for classes and variables.

If multiple threads are trying to operate simultaneously on the same java object, then there may be a chance of Data Inconsistency Problem.

To overcome this problem, we should go for synchronized keyword.

If a method or block is declared as synchronized  then at a time only one thread is allowed to execute that method or block on the given object, so that Data Inconsistency problem will be resolved.  

Workspace


Tags:  Virtusa



Most Popular Qs.

Latest MCQ Qs.