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...

  Take Quiz (Monsoon) Beta Version !!
  Do You have Qs. in your Mind ??? Ask it as Qs. or MCQs or create it as polling Live Now !!

JAVA Programming :: Basic Concepts

Home > JAVA Programming > Basic Concepts > General Questions

1 / 64

 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

2 / 64

 What will be the output of the below code segement:

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

3 / 64

 What will be the output of the below code segement:

public class SwitchTest {
public static void main(String argv[]) {
SwitchTest ms=new SwitchTest();
ms.display();
}
public void display() {
int k=10;
switch(k){

default:
System.out.println("This is the default output");
break;
case 10:
System.out.println("ten");
break;
case 20:
System.out.println("twenty");
break;
}
}
}

Answer: Option A

Explanation:

From the main() method when the control comes to the display() method, we find that k is initialized with value 10, after which we see a switch-case statement.

The switch takes the argument or the choice as k, or 10.

Hence, case 10 gets executed, and we get the output as ten.

Workspace


Tags:  Virtusa

4 / 64

 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 B

Explanation:

String cannot be saved as interger


Workspace


Tags:  Virtusa

5 / 64

 A programmer is designing a class to encapsulate the information about an inventory item.
A JavaBeans component is needed to do this.
The InventoryItem class has private instance variables to store the item information :

private int itemId;

Which method signature follows the JavaBeans naming standards for modifying the itemId instance variable ?

Answer: Option C

Explanation:

Here is no explanation for this answer

Workspace


Tags:  Virtusa

6 / 64

 Which interface does javutil.Hashtable implement?

Answer: Option A

Explanation:

The java.util.Hashtable class implements a hashtable, which maps keys to values.

To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode() method and the equals() method.

Workspace


Tags:  Virtusa

7 / 64

 What is the Output

class ArrayTest {
public static void main(String[] args) {
int[][] a1 = {{1,2,3},{4,5,6},{7,8,9,10}};
System.out.print(a1[0][2]+","+a1[1][0]+","+a1[2][1]);
}
}

Answer: Option A

Explanation:

In the given 2-dimensional array a1-

a1[0][2] = element in the first row and third column = 3

a1[1][0] = element in the second row and first column = 4

a1[2][1] = element in the third row and second column = 8

Hence, it prints 3,4,8.


Workspace


Tags:  Virtusa

8 / 64

 What will happen when you try to compile and run this code?

public class TGo implements Runnable{
public static void main(String argv[]){
TGo tg = new TGo();
Thread t = new Thread(tg);
t.start();
}
public void run() {
while(true) {
Thread.currentThread().sleep(1000);
System.out.println("looping while");
}
}
}

Answer: Option B

Explanation:

We will get a compile-time error in line no. 9, saying unreported exception InterruptedException; must be caught or declared to be thrown.

The sleep() method is responsible for generating the exception, and hence should be declared under try-catch block.

Hence, correct option is D.

Workspace


Tags:  Virtusa

9 / 64

 A method is declared to take three arguments. A program calls this method and passes only two arguments. What is the result?

Answer: Option A

Explanation:

Compilation fails.

Workspace


Tags:  Virtusa

10 / 64

 What can directly access and change the value of the variable name?
package test;
class Target {
public String name = "hello";
}

Answer: Option C

Explanation:

First of all, the class is default access specifier.

Now, the features of a class with default access specifier is-

1. It can be accessed by same package subclass.

2. It can be accessed by same package non-subclass.

Apart from this, no other subclass or non-subclass of some other packages can access this class.

Workspace


Tags:  Virtusa