Interview Questions → JAVA → Java Questionnaire III
Java Questionnaire III
class StringDemo
{
public void ConcatenateString()
{
String str = "this";
str += " is";
str += " the";
str += " demonstration";
System.out.println(str);
}
public void ConcatenateStringBuffer()
{
StringBuffer str = new StringBuffer("this");
str.append(" is");
str.append(" the");
str.append(" demonstration");
System.out.println(str);
}
}
class MainClass
{
public static void main(String args[])
{
StringDemo s = new StringDemo();
s.ConcatenateString();
s.ConcatenateStringBuffer(); // This method is quicker than the ConcatenateString() method
}
}
interface I
{
void Display();
}
class MainClass
{
public static void main(String args[])
{
I obj = new I();
}
}
class MultipleExceptionClass
{
void CallMethod()
{
try
{
// This condition will invoke ArithmeticException exception
//int x = 0;
//int y = 5/x;
// This condition will invoke ArrayIndexOutOfBoundsException exception
//int a[] = new int[2];
//int x = a[3];
}
catch(ArithmeticException e1)
{
System.out.println("Divided by zero exception");
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("Null exception");
}
}
}
class MainClass
{
public static void main(String args[])
{
MultipleExceptionClass c = new MultipleExceptionClass();
c.CallMethod();
}
}
public abstract class MyAbstractClass {
void Display()
{
// Method definition
}
}
class MainClass
{
public static void main(String args[])
{
MyAbstractClass c = new MyAbstractClass();
}
}
class MainClass
{
public static void main(String aa[])
{
double d = 22.55;
byte b = (byte)d;
System.out.println(b);
}
}
class MainClass
{
public static void main(String aa[])
{
Double dObj = new Double("22.55");
byte b = dObj.byteValue();
System.out.println(b);
}
}
class ThrowDemo
{
void CallMethod()
{
String str = null;
if(str == null)
{
throw new NullPointerException();
}
}
}
class MainClass
{
public static void main(String aa[])
{
try
{
ThrowDemo e = new ThrowDemo();
e.CallMethod();
}
catch(NullPointerException ex)
{
System.out.println("NullPointerException caught");
}
}
}
class InstanceOfGetClassDemo
{
public void CallMethod()
{
String s = "";
if(s instanceof String)
{
System.out.println("s is of type String");
}
System.out.println(s.getClass().getName());
}
}
public class MyClass {
public static void main(String args[])
{
InstanceOfGetClassDemo c = new InstanceOfGetClassDemo();
c.CallMethod();
}
}