Module 2 QB
Module 2 QB
Course Outcome Statements : After the successful completion of the course, the students will be able to
CO1 Demonstrate proficiency in writing simple programs involving branching and looping structures.
CO2 Design a class involving data members and methods for the given scenario.
CO3 Apply the concepts of inheritance and interfaces in solving real world problems.
CO4 Use the concept of packages and exception handling in solving complex problem.
CO5 Apply concepts of multithreading, autoboxing and enumerations in program development.
Q. Marks RBT
Questions & Answers Level
CO
No.
1 Explain Class with an example? 5 L2 2
A class is a template for an object, and an object is an instance of a
class.
A class is declared by use of the class keyword.
A simplified general form of a class definition is shown here:
class classname {
type instance-variable1;
// ...
type methodnameN(parameter-list) { // body of method
}
Simple Example:
class Box {
double width;
+
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Artificial Intelligence &Machine Learning
double height;
double depth;
class BoxDemo2 {
}
Output:
Volume is 3000.0
Volume is 162.0
As you can see, mybox1’s data is completely separate from the data
contained in mybox2.
What are Constructors? Explain two types of Constructors. 2
A constructor in Java is similar to a method that is invoked when an object of the
2 class is created. 6 L2
Unlike Java methods, a constructor has the same name as that of the class and
does not have any return type.
+
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Artificial Intelligence &Machine Learning
For example,
class Test {
Test() {
// constructor body
}
}
Here, Test() is a constructor. It has the same name as that of the class and doesn't
have a return type.
int i;
Output:
Java Programming Language
Python Programming Language
C Programming Language
Here, we are passing the single value to the constructor. Based on the
argument passed, the language variable is initialized inside the
constructor.
Explain static variable and static methods in JAVA. L2 2
When a member is declared static, it can be accessed before any
3 6
objects of its class are created, and without reference to any object.
Both methods and variables can be declared as static. Example of
a static member is main( ). main( ) is declared as static because it
+
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Artificial Intelligence &Machine Learning
static {
System .out .println ("Static block initialized.");
b = a*4;
}
public static void main (String args[]) {
meth (42);
}
}
Output:
Static block initialized.
x =42
a=3
b = 12
Write a program to perform Stack operation using proper class & L3 2
methods
4 8
import java.util.ArrayList;
import java.util.EmptyStackException;
+
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Artificial Intelligence &Machine Learning
import java.util.List;
class Stack {
private List<Integer> items;
public Stack() {
this.items = new ArrayList<>();
}
+
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Artificial Intelligence &Machine Learning
stack.push(1);
stack.push(2);
stack.push(3);
this is a keyword in Java which is used as a reference to the object of the current
class, with in an instance method or a constructor. Using this you can refer the
members of a class such as constructors, variables and methods.
Note − The keyword this is used only within instance methods or constructors
6 6
}
Call one type of constructor (parametrized constructor or default) from
other in a class. It is known as explicit constructor invocation.
class Student {
int age
Student() {
this(20);
}
Student(int age) {
this.age = age;
}
}
Example
Here is an example that uses this keyword to access the members of a class. Copy
and paste the following program in a file with the name, This_Example.java.
This_Example() {
System.out.println("This is an example program on keyword
this");
}
This_Example(int num) {
// Invoking the default constructor
this();
"+this.num);
Output:
This is an example program on keyword this
value of local variable num is : 20
value of instance variable num is : 10
Hi Welcome to Tutorialspoint
This is an example program on keyword this
value of local variable num is : 20
value of instance variable num is : 30
Hi Welcome to Tutorialspoint.
Write a JAVA program demonstrating Method Overloading. L3 2
• In Java, it is possible to define two or more methods within the
same class that share the same name, as long as their parameter
declarations are different, methods are said to be overloaded, and
the process is referred to as method overloading.
7 When an overloaded method is invoked, Java uses the type and/or 8
number of arguments as its guide to determine which version of
the overloaded method to actually call. Thus, overloaded methods
must differ in the type and/or number of their parameters. While
overloaded methods may have different return types, the return
type alone is insufficient to distinguish two versions of a method.
When Java encounters a call to an overloaded method, it simply
+
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Artificial Intelligence &Machine Learning
Output:
No parameters a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625
Distinguish between Method Overloading and Method Overriding in L2 2
8 8
Java, with suitable example.
+
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Artificial Intelligence &Machine Learning
Private and final methods can Private and final methods can’t
+ be overloaded. be overridden.
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Artificial Intelligence &Machine Learning
class Main {
String language;
obj1.getName();
obj2.getName();
}
}
Output:
Programming Language: Java
Programming Language: Python
+
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Artificial Intelligence &Machine Learning
11 8
+
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Artificial Intelligence &Machine Learning
Explanation:
Here is how it works. When fact( ) is called with an
argument of 1, the function returns 1; otherwise, it returns
the product of fact(n–1)*n. To evaluate this expression,
fact( ) is called with n–1. This process repeats until n
equals 1 and the calls to the method begin returning.
When you compute factorial of 3, the first call to fact( )
will cause a second call to be made with an argument of 2.
This invocation will cause fact( ) to be called a third time
with an argument of 1. This call will return 1, which is then
multiplied by 2 (the value of n in the second invocation).
This result (which is 2) is then returned to the original
invocation of fact( ) and multiplied by 3 (the original value
of n). This yields the answer,6. You might find it
interesting to insert println( ) statements into fact( ), which
will show at what level each call is and what the
intermediate answers are.
Explain two types of argument passing in Java. L2 2
There are two ways of passing an argument to a subroutine.
1. call-by-value: This approach copies the value of an argument into the
formal parameter of the subroutine. Therefore, changes made to the
parameter of the subroutine have no effect on the argument.
12 8
+
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Department of Artificial Intelligence &Machine Learning
Output
a and b before call: 15 20
a and b after call: 15 20
the operations that occur inside meth( ) have no effect on the
values of a and b used in the call; their values here did not change
to 30 and 10.
2. call-by-reference: In this approach, a reference to an argument (not the
value of the argument) is passed to the parameter. Inside the subroutine,
this reference is used to access the actual argument specified in the call.
This means that changes made to the parameter will affect the argument
used to call the subroutine.
Output:
ob.a and ob.b before call: 15 20
ob.a and ob.b after call: 30 10
As you can see, in this case, the actions inside meth( ) have
affected the object used as an argument.