Java Varargs or Variable Arguments MCQ Questions and Answers

0

 Java Varargs or Variable Arguments Interview MCQ Questions and Answers



Text Example

1) Java Varargs are applicable only for ___.

A) Constructors

B) Methods

C) Both Constructors and Methods

D) None

Answer [=]  C

Explanation: Only constructors and methods accept arguments. So, Java Varargs are applicable only to these.



2) A Java-Vararg is nothing but ____.

A) Variable number of arguments

B) Variable type of arguments

C) Variable size of arguments

D) All

Answer [=]  A

Explanation: A Java Vararg represents simply a variable number of arguments.




Java Varargs or Variable Arguments MCQ Questions and Answers
Java Varargs or Variable Arguments MCQ Questions and Answers




3) A Java vararg is a ____.

A) Method

B) Constructor

C) Variable

D) All

Answer [=]  C

Explanation:  A Java-Vararg represents a variable of a particular type.



4) A Java-Vararg can be of any type like primitive or object type. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]  A

Explanation:  Yes. A Vararg can be a non-primitive or object type also along with a primitive type like byte, short, int, long, float, double etc.




5) A Java Vararg or Variable Argument can come at any position in a method or constructor. State TRUE or FALSE.

A) FALSE

B) TRUE

C) -

D) -

Answer [=]  B

Explanation:  A Vararg can come only as the last argument in the list of arguments of a constructor or method.




6) What is the output of the below java program with varargs?

public class Varargs1

{

static void displayStudents(String... stu)

{

for(String s: stu)

System.out.print(s + " ");

}

public static void main(String args[])

{

displayStudents("Bean", "Atkinson", "Milton");

}

}

A) Bean Bean Bean

B) null null null

C) Bean Atkinson Milton

D) Compiler error

Answer [=]  C

Explanation:   In the above example, the Vararg variable "stu" is of String array type. So, we have used a FOR loop to go through all the elements. We also use a NORMAL FOR loop with an index starting from 0.




7) What is the output of the below Java program with Variable arguments?

public class Varargs2

{

void attendance(String... allStu)

{

System.out.println("Attended: " + allStu.length);

}

void attendance(boolean... all)

{

System.out.println("Attended: " + all.length);

}

public static void main(String args[])

{

new Varargs2().attendance();

}

}

A)

Attended: 0

B)

Attended: 0

Attended: 0

C) No Output

D) Compiler Error

Answer [=] D

Explanation:  Observe that there is no default constructor with 0 arguments. When no argument is passed, the compiler can not choose which attendance() method to choose. So it gives an error.



8) Which is the error thrown when two methods with varargs look the same to the compiler?

A) The method is ambiguous

B) The method is difficult to choose

C) The method signature is not correct

D) None

Answer [=]  A

Explanation:  The compiler simply gives an error when two methods look the same to the compiler for calls at compile time.




9) What is the output of the below Java program with Varargs?

public class Varargs3

{

Varargs3(int... dates)

{

System.out.println("Inside Varargs(int...)");

}

Varargs3(boolean... yesno)

{

System.out.println("Inside Varargs(float...)");

}

public static void main(String[] args)

{

new Varargs3();

}

}

A)

Inside Varargs(int...)

B)

Inside Varargs(boolean...)

C)

Inside Varargs(int...)

Inside Varargs(boolean...)

D) Compiler error

Answer [=]  D

Explanation: As the two constructors have the same method signature with zero arguments, the compiler sees those as the same. So, it throws an error saying the Constructor is ambiguous.



10) What is the output of the below Java program with Varargs?

public class Varargs4

{

Varargs4(int... carnums)

{

System.out.println("Inside Varargs(int...)");

}

Varargs4(float... prices)

{

System.out.println("Inside Varargs(float...)");

}

public static void main(String[] args)

{

new Varargs4();

}

}

A)

Inside Varargs(int...)

B)

Inside Varargs(int...)

Inside Varargs(float...)

C) No output

D) Compiler error

Answer [=]  A

Explanation: In the above example, both constructors are overloading one another. In such cases, the compiler chooses the Constructor or Method with a lower sized data type as an argument.



11) What is the output of the below code snippet?

public class Varargs5

{

Varargs5(int...weights, boolean yesno)

{

System.out.println("AMAZON");

}

public static void main(String[] args)

{

//new Varargs5(20, true);

}

}

A) No output

B) Error: The variable argument type int of the method Varargs5 must be the last parameter

C) Error: Varargs do not allow other data types

D) None

Answer [=]  B

Explanation: Yes. The compiler throws errors "Unresolved Compilation Problem" and "The variable argument type of the method or constructor must be the last parameter"



12) Which is the operator used to represent a Vararg type in a method or constructor in Java?

A) One Dot (.)

B) Two Dots (..)

C) Three Dots (...)

D) DOT DOT DOT COMMA (...,)

Answer [=]  C

Explanation:  The data type (primitive or object) immediately followed by three dots and a variable name separated by a space create a Variable Argument.



13) How many maximum numbers of Varargs or Variable-Arguments can be there in a method or a constructor in Java?

A) 1

B) 2

C) 8

D) 16

Answer [=]  A

Explanation:  Yes, only one. Because a VARARG can be present only as the last parameter or argument.




14) What is the maximum number of methods or constructors with Varargs in a single Java class?

A) 1

B) 2

C) 8

D) There is no limit

Answer [=]   D

Explanation:  Yes. There is no limit. There can be any number of methods or constructors with one Vararg per each method or constructor.








Java Inheritance Interview MCQ Questions and Answers


Text Example

1) What are the features of an Object Oriented Programming (OOPs)?

A) Inheritance

B) Encapsulation

C) Polymorphism

D) All the above

Answer [=]

D

2) What are the features reused using Inheritance in Java?

A) Methods

B) Variables

C) Constants

D) All the above

Answer [=]

D

Explanation:

Variables and Methods are reused through inheritance. Constants are nothing but variables only if they hold some value.

3) The class that is being inherited or subclassed is called ___.

A) Subclass

B) Superclass

C) -

D) -

Answer [=]

4) The class that inherits an already defined class is called ___.

A) Subclass

B) Superclass

C) -

D) -

Answer [=]

A

Explanation:

Subclass

5) Java language supports ___ type of inheritance.

A) Multiple Inheritance

B) Multi-Level Inheritance

C) -

D) -

Answer [=]

B

Explanation:

Multi-Level Inheritance is somewhat complicated.

6) You should use Inheritance when there is an IS-A relationship between classes. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]

A

Explanation:

Yes. One simple example is ANIMAL Superclass and HORSE Subclass. HORSE is-a/is-an ANIMAL.

7) What are the types of Inheritances (Whether Java supports or not) available in Object-Oriented Programming Languages?

A) Single Inheritance

B) Multi-Level Inheritance, Hierarchical Inheritance

C) Multiple Inheritance, Hybrid Inheritance

D) All the above

Answer [=]

D

Explanation:

Java supports extending from only one Superclass. Multilevel inheritance is completely supported by Java. Whereas Multiple and Hybrid inheritances are based on Multiple-Superlclasses scenario and hence not supported by Java.

8) In a Single inheritance, Class B inherits only from Class A. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]

A

Explanation:

True.

9) In a Multi Level Inheritance Class-C inherits from Class-B and Class-B inherits from Class-A. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]

A

Explanation:

True

10) In a Multi-Level Inheritance in Java, the last subclass inherits methods and properties of ____.

A) Only one immediate Superclass

B) Few classes above it.

C) All classes above it

D) None

Answer [=]

C

Explanation:

Yes. The last class inherits all of the properties and methods of all of the classes above it in the chain.

11) When a Class inherits two superclasses (not in Java), it is called ____ inheritance.

A) Multilevel inheritance

B) Single Inheritance

C) Multiple Inheritance

D) None

Answer [=]

C

Explanation:

Multiple Inheritance

12) A Subclass can become a Superclass to another class extending from it in Java. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]

A

Explanation:

Yes, true.

13) You can not inherit a Superclass'es constructor even after using inheritance in Java. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]

A

Explanation:

Yes. True. A Constructor is class-specific. It is not like a method that can be shared.

14) Find Superclass and Subclass in the below Java code snippet?

class B

{

void show(){}

}

class A

{

void hide(){}

}

A) B is superclass and A is subclass.

B) A is superclass and B is a subclass.

C) There is no superclass or subclass present.

D) None

Answer [=]

C

Explanation:

As there is no use of the "extends" keyword, inheritance does not come into the picture. So, there is no superclass or subclass present in the above example.

15) Find Superclass and Subclass in the below Java program?

class Liquid

{

void pour(){}

}

class Juice extends Liquid

{

void filter(){}

}

A) The Liquid is a superclass and Juice is a subclass.

B) The Liquid is a Subclass and Juice is a Superclass.

C) There is no superclass or subclass

D) None

Answer [=]

A

Explanation:

As the Juice class is extending from the Liquid class, Juice is a subclass and Liquid is a superclass. Simply put, the class before the "extends" keyword is always a Subclass.

16) Which is the keyword used to implement inheritance in Java?

A) extends

B) implements

C) instanceof

D) None

Answer [=]

A

Explanation:

The keyword "extends" tells the compiler that the class on the left side is subclassing the class on the right side.

17) What is the output of the below Java program with inheritance?

class Sweet

{

void price()

{

System.out.print("Sweet=$10 ");

}

}

class Sugar extends Sweet

{

void price()

{

super.price();

System.out.print("Sugar=$20");

}

}

public class JavaInheritance1

{

public static void main(String[] args)

{

Sugar su = new Sugar();

su.price();

}

}

A) Sweet=$10 Sugar=$20

B) Sweet=$10 Sugar=$10

C) Sweet=$20 Sugar=$20

D) Compiler error

Answer [=]

A

Explanation:

Notice the use of the keyword "super". Using this keyword, you can call superclass's methods and variables.

18) Can you call it a full-fledged inheritance of using ABSTRACT classes and INTERFACES in Java?

A) NO

B) YES

C) -

D) -

Answer [=]

A

Explanation:

No. Abstract classes and Interfaces do not define a class completely. So, it is not called a full-fledged inheritance of using those.

19) To control inheritance to different classes and levels, Java provides ____.

A) Return types like the void, int, float, double and other object types

B) Static keyword

C) Access modifiers like default, public, protected, private

D) None

Answer [=]

C

Explanation:

Access modifiers like default (not a keyword), public, protected and private changed the visibility of a method, variable or a class. Even the keyword "package" also allows grouping of classes and control inheritance levels to some extent.

20) To stop or block inheriting a given class, the ___ keyword is used before the class.

A) static

B) private

C) final

D) none of the above

Answer [=]

C

Explanation:

You can not subclass a class that is marked FINAL.

final class CLASS_NAME //Can not subclass

{

}

class SUBCLASS extends CLASS_NAME //ERROR

{

}

Text Example

1) What is the maximum number of levels possible in a Multilevel Inheritance in Java?

A) 8

B) 16

C) 32

D) No maximum level

Answer [=]

D

Explanation:

There is no limit to the number of levels in a multilevel inheritance chain in Java.

2) What is the output of the below java program with Constructors and Inheritance?

class Processor

{

Processor()

{

System.out.print("Inside Processor() Constructor. ");

}

}

class I3Processor extends Processor

{

I3Processor()

{

System.out.print("Inside I3Processor() Constructor. ");

}

}

class I5Processor extends I3Processor

{

I5Processor()

{

System.out.print("Inside I5Processor() Constructor. ");

}

}

public class JavaInheritance2

{

public static void main(String[] args)

{

I5Processor i5 = new I5Processor();

}

}

A) Inside I5Processor() Constructor. Inside I3Processor() Constructor. Inside Processor() Constructor.

B) Inside I5Processor() Constructor. Inside I5Processor() Constructor. Inside I5Processor() Constructor.

C) Inside Processor() Constructor. Inside I3Processor() Constructor. Inside I5Processor() Constructor.

D) Compiler error

Answer [=]

C

Explanation:

The example demonstrates the Invoking of constructors using a Multilevel Inheritance. Always, the default superclass's constructor is invoked. And then, the subclass's constructor is invoked.

Processor <-- I3Processor <-- I5Processor

3) What is the output of the below Java program with Constructors using Inheritance?

class Ant

{

Ant(String name)

{

System.out.print("Inside Ant(String) Constructor. ");

}

}

class WildAnt extends Ant

{

WildAnt()

{

System.out.print("Inside WildAnt() Constructor. ");

}

}

public class Inheritance3

{

public static void main(String[] args)

{

WildAnt wa = new WildAnt();

}

}

A) Inside WildAnt() Constructor.

B) Inside Ant(String) Constructor. Inside WildAnt() Constructor.

C) Inside WildAnt() Constructor. Inside WildAnt() Constructor.

D) Compiler error

Answer [=]

D

Explanation:

Compiler throws an error "implicit constructor of Ant class is undefined". Once you define a Constructor with some arguments, the compiler does not add a default constructor with zero arguments. To subclass a class, defining a default constructor is mandatory.

4) If a class is not subclassed by any class, then defining a default constructor is not mandatory in Java. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]

A

Explanation:

True.

5) What is the output of the below Java program?

final class Bus

{

void show()

{

System.out.print("Generic Bus. ");

}

}

class ElectricBus extends Bus

{

void show()

{

System.out.println("Electric Bus. ");

}

}

public class Inheritance4

{

public static void main(String[] args)

{

ElectricBus eb = new ElectricBus();

eb.show();

}

}

A) Generic Bus

B) Electric Bus

C) Generic Bus. Electric Bus.

D) Compiler error.

Answer [=]

D

Explanation:

Notice the use of the keyword "final" before the superclass BUS. You can not subclass a class that is marked final.

6) A Superclass reference can refer to a Subclass Object without casting. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]

A

Explanation:

Yes.

7) A superclass reference can not be used to invoke a method or variable of the subclass. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]

A

Explanation:

Yes. A superclass reference knows only about the methods and properties of the same class but not the subclass.

8) A subclass object can be used to invoke (call) a method or property of the superclass. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]

A

Explanation:

Yes, True.

9) What is the output of the below Java program on the references of Superclass and Subclass?

class Food

{

void show()

{

System.out.print("FOOD ");

}

}

class Bread extends Food

{

void toast()

{

System.out.print("TOASTED ");

}

}

public class Inheritance5

{

public static void main(String[] args)

{

Food foo = new Food();

foo.show();

Food foo2 = new Bread();

foo2.show();

Bread br = new Bread();

br.toast();

br.show();

}

}

A) FOOD FOOD FOOD FOOD

B) FOOD FOOD TOASTED FOOD

C) FOOD TOASTED FOOD FOOD

D) Compiler error

Answer [=]

B

Explanation:

You can only invoke methods of the Superclass using a Superclass reference variable pointing to a Subclass object.

10) What is the output of the below Java program using Inheritance?

class Furniture

{

void show()

{

System.out.println("Made of Wood. ");

}

}

class Sofa extends Furniture

{

void addCushion()

{

System.out.println("Added. ");

}

}

public class Inheritance6

{

public static void main(String[] args)

{

Furniture fur = new Sofa();

fur.addCushion();

}

}

A) Added.

B) No output

C) Added. Made of Wood.

D) Compiler error

Answer [=]

D

Explanation:

Yes. The compiler throws an error saying "The method addCushion() is undefined for the type Furniture". It means that you can not call a method of subclass using a superclass reference even though it is pointing to a subclass object.

Post a Comment

0 Comments
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !