Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Ques�on 1.

(b) How is a constructor different from a member func�on of a class?


Ans: A constructor of a class is different from a member func�on in the following ways:
Constructor Member Func�on
1. A constructor of a class bears the same name A member func�on of a class can have any
as that of the class. name.
2. It does not have any return type. It always has a return type.
3. It gets automa�cally called when the object It needs to be manually called when needed.
of the class gets created.

(e) State any two advantages of if else construct over switch case.
Ans: The two advantages of the if-else construct over the switch case are:
(i) The if-else construct can handle a range of values whereas switch case can handle only single
values.
(ii) The if-else construct can evaluate expressions of any type whereas switch case can evaluate
expressions of int, char and String type only.
(iii) The if-else construct supports the use of all rela�onal operators (>, <, >=, <= and ==) whereas the
switch statement works by comparing the switch expression with the case values for equality.

Ques�on #2.
(c) Consider the following elements:
638421
Show how the following elements will appear a�er each pass of Bubble sort.
Ans: When sorted in ascending order, the arrangement of the array elements are as follows:
A�er Pass 1:
364218
A�er Pass 2:
342168
A�er Pass 3:
321468
A�er Pass 4:
213468
A�er Pass 5:
123468
Thus, the array elements get sorted in ascending order a�er 5 passes of the Bubble sort algorithm.

Ques�on 3.
(e) Dis�nguish between call by value and call by reference. [two differences in tabular order]
Call by value Call by reference
1. In this type of a func�on call, only the values In this type of func�on call, the reference
of the variables are sent to the calling func�on. to the actual arguments are sent to the
calling func�on.
2. In this type of func�on call, any changes made In this type of call, any changes made to the
inside the func�on do not affect the original values func�on parameter(s) inside the func�on
of the variables. will affect the values of the original
variables.
(h) State the use of the keyword new.
Ans: The new keyword in Java, is used for:
(i) Crea�ng object of a class.
(ii) Allocate memory for the array.

(i) What is polymorphism? How does Java implement polymorphism? Explain briefly.
Ans: Polymorphism is one of the four fundamental principles of Object-Oriented Programming (OOP). It
refers to the ability of a variable, func�on, or object to take on mul�ple forms.
In Java, polymorphism is implemented in two ways: Method overloading and Method overriding.
1. Method overloading is when mul�ple methods have the same name but different parameters.
2. Method overriding is when a subclass provides a specific implementa�on of a method that is already
provided by its parent class.

You might also like