School of Computer Science Engineering and Technology
Course- BTech Type- Core
Course Code- CSET Course Name- Object Oriented Programming Using Java
Year- First Semester- Even Batch- BTech 2nd Semester
Tutorial-5
Tutorial No. Name CO1 CO2 CO3
1 Basics -- --
Objective: The main objective of this tutorial is to learn about methods, static methods,
polymorphism in terms of method overloading, and object creation.
Q1. What is the following code doing, what will be its output?
class Test {
// Returns an array such that first element
// of array is a+b, and second element is a-b
static int[] getSumAndSub(int a, int b)
{
int[] ans = new int[2];
ans[0] = a + b;
ans[1] = a - b;
// returning array of elements
return ans;
}
// Driver method
public static void main(String[] args)
{
int[] ans = getSumAndSub(100, 50);
[Link]("Sum = " + ans[0]);
[Link]("Sub = " + ans[1]);
}
}
Ans: Array used to return 2 variables so output will be
150
50
Q2. What is effect of below code, show its output?
public class Main {
public static void Reverse(int arr[], int i, int j)
{
if(i < j && i < [Link] && j >= 0)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
Reverse(arr, i+1, j-1);
School of Computer Science Engineering and Technology
public static void main(String args[]) {
int arr[] = new int []{3,7,1,10,2,6,12};
for(int i=0; i < [Link]; i++)
[Link](arr[i] + " ");
[Link]("");
Reverse(arr, 0, [Link]-1);
for(int i=0; i < [Link]; i++)
[Link](arr[i] + " ");
}
}
Ans: the code reverses the array using recursive function, output:
3 7 1 10 2 6 12
12 6 2 10 1 7 3
Q3. What is need of 2nd class: MultiDivAdd in this code and what will be the output?
class MultiDivAdd {
int mul; // To store multiplication
double div; // To store division
int add; // To store addition
MultiDivAdd(int m, double d, int a)
{
mul = m;
div = d;
add = a;
}
}
class Test {
static MultiDivAdd getMultDivAdd(int a, int b)
{
// Returning multiple values of different
// types by returning an object
return new MultiDivAdd(a * b, (double)a / b, (a + b));
}
// Driver code
public static void main(String[] args)
{
MultiDivAdd ans = getMultDivAdd(10, 20);
[Link]("Multiplication = " + [Link]);
[Link]("Division = " + [Link]);
[Link]("Addition = " + [Link]);
}
School of Computer Science Engineering and Technology
Ans: Class MultiDivAdd used to return 3 values, so output:
200
0.5
30
*Note: Please discuss the concept of access specifiers and static keyword for methods.
Q4. What is the below code doing?
public class Main {
public int Power(int x, int n)
{
if(n == 0)
return 1;
if(n%2 == 1)
{
int y = Power(x, (n-1)/2);
return x*y*y;
}
else
{
int y = Power(x, n/2);
return y*y;
}
}
public static void main(String args[]) {
Main pow = new Main();
[Link]([Link](2,5));
[Link]([Link](2,6));
}
}
Ans: The code recursively calculates power of any number, output:
32
64
Q5. What is below concept called for using same function name for different arguments,
what is its output?
public class Main
{
public int sum(int x, int y) { return (x + y); }
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
School of Computer Science Engineering and Technology
// Overloaded sum(). This sum takes two double
// parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Main s = new Main();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
[Link]([Link](10.5, 20.5));
}
}
Ans: This is for method overloading. Also explain basics of default constructor.
Output:
30
60
31.0
Q6. What will be output of below code?
public class Main {
// Normal main()
public static void main(String[] BU) {
[Link]("Hi BU (from main)");
[Link]("BU");
}
// Overloaded main methods
public static void main(String arg1) {
[Link]("Hi, " + arg1);
[Link]("Dear BU","My BU");
}
public static void main(String arg1, String arg2) {
[Link]("Hi, " + arg1 + ", " + arg2);
}
Ans: it shows how to overload main method, the output will be:
Hi BU (from main)
Hi, BU
Hi, Dear BU, My BU
School of Computer Science Engineering and Technology
Q7. What is use of MyInt class and what will be its output?
class MyInt
{
private int val;
public MyInt(int x)
{
val = x;
}
public void setValue(int newval)
{
val = newval;
}
public int getValue()
{
return val;
}
}
public class Main
{
public static void swap(Integer i, Integer j) {
Integer temp = [Link](i);
i = j;
j = temp;
}
public static void swap1(MyInt i, MyInt j) {
MyInt temp = new MyInt([Link]());
[Link]([Link]());
[Link]([Link]());
}
public static void main(String[] args) {
Integer i = [Link](10);
Integer j = [Link](20);
swap(i, j);
[Link]("i = " + i + ", j = " + j);
MyInt i1 = new MyInt(10);
MyInt j1 = new MyInt(20);
swap1(i1, j1);
[Link]("i = " + [Link]() + ", j = " + [Link]());
}
}
Ans: MyInt class helps to get the modified value outside main also, so output:
i = 10,j = 20
I = 20,j = 10
*Note: Discuss access modifier, constructor, methods.
School of Computer Science Engineering and Technology
Q8. What will be the Output of the below code:
public class prob4 {
public static void main(String[] args) {
int x = 20;
[Link](x);
}
static {
int x = 10;
[Link](x + " ");
}
}
OUTPUT –
10 20
*Note: discuss static block.
Q9. What will be output of below code?
class Test1 {
int x = 10;
public static void main(String[] args) {
[Link](x);
}
static {
[Link](x + " ");
}
}
OUTPUT –
Error: Non static field ‘x’ cannot be referenced from a static context