Basics of Java Programming

This quiz tests fundamental concepts of Java programming essential for beginners, including object-oriented principles, data types, control structures, inheritance, interfaces, garbage collection, and core Java language features.

14 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

What is the output of following Java program?

public class Test
{
public static void main(String[] args)
{
double i=45.6567;
float j=i;
System.out.println( j );
}
}

  1. 45
  2. 45.6567
  3. 0.6567
  4. Run time Exception
  5. None of the above
Question 2 Multiple Choice (Single Answer)

Which of the following statements is/are true regarding JAVA?

  1. JAVA is object-oriented.
  2. JAVA supports multi-threading.
  3. JAVA is platform independent.
  4. JVM provides run time environment to execute java byte code.
  5. All of the above
Question 3 Multiple Choice (Single Answer)

Choose the correct statement(s) regarding garbage collection in Java.

  1. Garbage collection can be forced explicitly.
  2. Garbage collection is done by JVM.
  3. User can request JVM for Garbage collection explicitly calling gc() method.
  4. Both 1 and 2 are true.
  5. Both 2 and 3 are true.
Question 4 Multiple Choice (Single Answer)

Read the following statements and choose the correct option.
P) Abstract class can be instantiated.
Q) Abstract class must contain at least one abstract method.
R) All variables declared in interface are implicitly static in nature.
S) All methods declared in interface are implicitly abstract in nature.

  1. All the statements are true.
  2. Only R, S are true.
  3. Only Q, R, S are true.
  4. Only Q is true.
  5. Only P, R, S are true.
Question 5 Multiple Choice (Single Answer)

Read the following statements and choose the correct option.

P) Variable or function declared in a class as private cannot be accessed outside the class even though the class is inherited by another class.
Q) Public variables or functions of a class can be accessed to any class.
R) When a class is declared as Static , it doesn't need an instance to access it members.
S) When a member of class is declared as protected access modifier, then it can accessed by its child classes.

  1. All the statements P, Q, R and S are true.
  2. Only P, Q and R are true.
  3. Only S is true.
  4. Only R and S are true.
  5. None of the above
Question 6 Multiple Choice (Single Answer)

What is the output of the following Java program?

class A
{
static int a=20,b=30;
A()
{
a++;
b++;
}
public void func()
{
System.out.println("a="+a+" b="+b);
}
}
public class Test
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
a1.func();
a2.func();
}
}

  1. a=21 b=31
    b=22 b=32
  2. a=22 b=32
    b=22 b=32
  3. a=22 b=32
    b=21 b=31
  4. a=20 b=30
    b=20 b=30
  5. None of the above
Question 7 Multiple Choice (Single Answer)

Which of the following is a valid identifier in Java programming?

  1. this
  2. 3name
  3. _name1
  4. #name1
  5. None of the above
Question 8 Multiple Choice (Single Answer)

What is the output of the following Java program?

class A
{
A()
{
System.out.println("Constructor A");
}
public void func()
{
System.out.println("Member function A");
}
}
class B extends A
{
B()
{
System.out.println("Constructor B");
}
public void func()
{
System.out.println("Member function B");
}
}
public class Hello
{
public static void main(String args[])
{
B b=new B();
b.func();
}
}

  1. Constructor A
    Constructor B
    Member function B
  2. Constructor B
    Constructor A
    Member function B
  3. Constructor A
    Constructor B
    Member function A
  4. Constructor B
    Constructor A
    Member function A
  5. None of the above
Question 9 Multiple Choice (Single Answer)

Which of the following statements is true?

  1. StringBuilder class is faster than StringBuffer class.
  2. StringBuilder is thread safe.
  3. StringBuffer str=new StringBuffer("hello");
    //creates an immutable string object "str" which contains "hello"
  4. StringBuilder is synchronized.
  5. None of the above statements is true
Question 10 Multiple Choice (Single Answer)

What is the output of the following Java program?

interface A{

}
class B implements A
{

}
class C extends B
{

}
public class test
{
public static void main(String[] args)
{
A c=new C();
System.out.println(c instanceof B);
System.out.println(c instanceof A);
}
}

  1. true
    true
  2. true
    false
  3. false
    true
  4. false
    false
  5. Error in the program
Question 11 Multiple Choice (Single Answer)

What is the output of the following Java program?

public class test
{ public static void main(String[] args)
{
try
{
int a[]=new int[8];
a[0]=0;
a[1]=1;
for(int i=2;i<=a.length-1;i++)
{
a[i]=a[i-1]+a[i-2];
System.out.println(a[i]+" ");
}
}
catch(Exception e)
{
System.out.println(e);
}
}

}

  1. 1 2 3 4 5 6
  2. 1 2 3 5 8
  3. 1 2 3 5 8 13
  4. 1 2 3 5 8 13 21
  5. Error in the program
Question 12 Multiple Choice (Single Answer)

What is the ouput of the following Java program?

interface A{
double pi=3.14;
public double Area(int r);
}
class C implements A
{
double pi=3.1;
public double Area(int r)
{
return(pirr);
}
}
public class test
{
public static void main(String[] args)
{
C c=new C();
System.out.println(c.Area(2));
}
}

  1. 12.56
  2. 12.4
  3. 12
  4. Error in the program
  5. None of the above
Question 13 Multiple Choice (Single Answer)

What is the ouput of the following Java program?

class A{
A()
{
System.out.println("without argument in class A");
}
A(int a)
{
System.out.println("with argument in class A");
}
}
class B extends A
{
B()
{
System.out.println("without argument in class B");
}

B(int a)

{
System.out.println("with argument in class B");
}
}
public class test
{
public static void main(String[] args)
{
B a=new B(1);
}
}

  1. with argument in class A
    without argument in class B
  2. without argument in class A
    without argument in class B
    with argument in class B
  3. without argument in class A
    with argument in class B
  4. without argument in class A
    without argument in class B
    with argument in class B
    with argument in class B
  5. None of the above
Question 14 Multiple Choice (Single Answer)

What is the output of the following Java program?

class A
{
public StringBuilder f1()
{
StringBuilder b=new StringBuilder("This is Java Program");
b.replace(8, 12, "Example Java");
return b;
}
}
public class Test
{
public static void main(String[] args)
{
A a=new A();
System.out.println(a.f1());
}
}

  1. Example Java
  2. This is Program
  3. This is Example Java Program
  4. This is Example Java
  5. None of the above