Programming Interview Questions
What will be the output of the below given program?
public class Test {
static {
i=5;
}
static int i;
public static void main(String[] args) {
System.out.println("i value is "+i);
}
}
Options :
0
5
Compilation Error
1
Answer : b. 5
What will be the output of the below given program?
public class Test {
static int i=5;
public static void main(String[] args) {
Test test = null;
System.out.println("i value is "+test.i);
}
}
Options :
0
5
Compilation Error
NullPointerException
Answer : b. 5
This is because static variables are related to class and not instance. Hence, the instance value doesn't hold good if a static variable is tried to be accessed.
What will be the output of the below given program?
class Father {
protected Father(String str) {
System.out.println("Created a Father " +str);
}
}
public class Child extends Father {
private Child() {
System.out.println("Inside child");
}
public static void main(String args[]) {
super("Hi");
new Child();
}
Options :
Inside Child
Created a Father
Compilation error
Created a Father Hi Inside Child
Answer : c. Compilation error
As per the rule always the 'parent default' constructor should be present if a 'parameterized' constructor is created. In this case create a 'default' constructor as 'Father' class is missing.
What is a WeakHashMap?
Options :
A hashtable-based Map implementation with weak keys
A list with weak references to objects
A hasttable map with duplictated keys
A general purpose hashtable-based implementation to better store.
Answer : a. A hashtable-based map implementation with weak keys.
Which of the following is/are true?
Options :
if("String ".trim() == "String")
if(" String ".trim() == "String")
if("String".trim() == "String")
if("Str ing ".trim() == "String")
Answer : c.
trim() function is only performed if blankspaces are present at the beginning or end. If not present it will return the same string. Hence the function will return true. But if the string contains blankspaces, subString() is called to trim the string and return a new String using 'new' operator.
Which of the following lines allow main method to be executed by ClassLoader?
Options :
public static String main(String args[]);
protected static void main(String args[]);
final public static void main(String args[]);
public static void main();
private static void main(String args[]);
public void main(String args[]);
Answer : 3.
As only final, public, static and void is allowed with main method.
What is the output of the below given program?
public class FinalVar {
private int final i;
public static void main(String args[]) {
System.out.println(new FinalVar().i);
}
}
Options :
1
0
RunTimeException occurs
Compile time error 'i should be initialized
Answer : d.
'Final' variable needs to be initialized always.
What is the output of the below given program?
public class Question05 {
public static void main(String[] args) {
Question05Sub myref = new Question05Sub();
try{
myref.test();
}
catch(IOException ioe){}
}
void test() throws IOException {
System.out.println("In Question05_JBK");
throw new IOException();
}
}
class Question05Sub extends Question05 {
void test() {
System.out.println("In Question05Sub");
}
}
}
Options :
In Question05
In Question05Sub
Compilation error
In Question05
Answer : c.
If the 'parent' method throws a 'Checked Exception' then the 'child' class overriding it can throw same exception or unchecked exception or any subclass of the Exception thrown (in this case subclass of IOException).
What is the output of the below given program?
public class ThreadClass {
public static void main(String[] args) {
new ThreadClass().doSomething();
}
public void doSomething() {
int i=5;
Thread t = new Thread(new Runnable() {
public void run() {
for(int j=0;j<=i;j++) {
System.out.print(" "+j);
}
}
});
t.start();
}
}
Options :
Print 0,1,2,3,4
Print 1,2,3,4
Compilation error
throws RunTimeException
Answer : c.
The local variable 'i' cannot be accessed directly inside inner class. To make it accessible it should be declared as 'final'.
What is the output of the below given program?
public class Parent {
static {
System.out.println("Inside Parent static");
}
{
System.out.println("Inside Parent init");
}
public Parent()
{
System.out.println("Parent Const");
}
public static void main(String args[])
{
new MyChild();
}
}
class MyChild extends Parent{
static {
System.out.println("Inside Child static");
}
{
System.out.println("Inside Child init");
}
public MyChild(){
System.out.println("Child Const");
}
}
Options :
Inside Parent static
Inside Child static
Inside Parent init
Parent Const
Inside Child init
Child ConstInside Parent static
Inside Child static
Inside Parent init
Inside Child init
Parent Const
Child ConstInside Parent static
Inside Child static
Inside Parent init
Inside Child init
Child Const
Parent ConstInside Parent init
Inside Child init
Inside Parent static
Inside Child static
Child Const
Parent Const
Answer : a.
What is the output of the below given program?
public class Test {
public static void main(String args[]) {
int i = 132;
short s = 15;
byte b = (byte)i;
int x = b + s;
System.out.println(x);
}
}
Options :
147
-109
Compilation error
Rutime error
Answer : b. -109
What is output of the below given program?
If an inner class enclosed with an outer class is compiled, then there is one ".class" file and a inner class ".class" file for each outer class. e.g.
public class Test{
public static void main(String args[]){
int i = 132;
List list = new ArrayList();
list.add(new Object());
list.add("Hi");
list.add(i);
System.out.println(list.get(1));
}
}
Options :
Hi
Compilation Error
Runtime Error
132
Answer : a. Hi
What is output of the below given program?
public class Widening1 {
public void f1(Object o1)
{
System.out.println("Inside f1 with object as argument");
}
public void f1(String s)
{
System.out.println("Inside f1 with String as argument");
}
public static void main(String[] args)
{
new Widening1().f1(null);
}
}
Options :
Inside f1 with String as argument
Inside f1 with object as argument
Compilation error
Runtime error
Answer : b. Inside f1 with String as argument