You are on page 1of 24

Short Questions: //green—lab expts//,//yellow--available

1. Explain Basic concepts of OOPs.

2. Distinguish between Procedural oriented programming and Object oriented programming.

3. Discuss the advantages of StringBuffer class over String class.

4. Develop a java program to demonstrate static variable, static method.

5. Discuss the usage of ‘this’ keyword and demonstrate with syntax.

6. Define Inheritance. Explain the different types of inheritance.

7. Write a Java Program to find sum of elements in a given array.


8. Write a Java program to find the maximum number of two integer values, two double
values and three double values by using method overloading mechanism.

9. Define Polymorphism with an Example.

10. Distinguish between multiple inheritance and multilevel inheritance.

11. Write a Java Program to read different data type values form user by using
Scanner class and print the same.
12. Write a simple java program to add two nmbers.

Long Questions:

1. List and explain the Scanner class methods used for reading different types of data.
2. Analyze the characteristics of final methods and final classes with suitable examples.
3. Compare and Contrast entry controlled and exit controlled loops with a suitable
example for each.
4. Analyze the characteristics of static methods with suitable examples.
5. List and explain the different integer types of Java along with the number of bytes they
occupy and their range.
6. Demonstrate the concept of method overriding with an example.
7. Analyze the process of achieving Call by Reference in java with an example.
8. Discuss about the various Access specifiers and their significance with an example.
9. Analyze the process of achieving Call by Reference in java with an example.
10. Bring out the uses of final keyword.
11. Develop a program in Java to print the program as output
12. write the difference between overloading and overriding.
13. Develop a java program to define a class called Student with 2 fields (Name, Id) and two
constructors one a default constructor and the other, a parameterized constructor to initialize
the fields. Incorporate a show() method to display the field values. Create objects for the class
and analyze the steps of the program.
14. Bring out the significance of labelled break and labelled continue with a suitable
example for each.
15. Analyze the application of this keyword to resolve naming conflicts.

Very Long Questions:

1. Define a class Area with two derived classes Square and Rectangle each with a method
with same name findArea() that compute and return the area of rectangle, and square
respectively. Incorporate a main method to invoke and to retrieve the results of these
methods.
2. Develop a program to perform Bitwise Shift operations (<<, >> and >>>) on suitable
data. Employ the Integer.toBitString() method to display the results.
3. Bring out the memory allocation procedure for non-static and static members of a class
along with the difference in procedure to invoke non-static and static methods.
Appreciate the characteristics of static fields and static methods with suitable examples.
4. Develop a java program to find the sum and average of n elements. Use a 1D array to
implement the same
5. Discuss the concept of Method overloading? Analyze with an example
Develop a java program to search for an element in an array of n elements.
6. Implement the Linear Search Technique

Solutions

1.Explain Basic concepts of OOPs.

A: OOP concepts in Java are the main ideas behind Java’s Object Oriented
Programming. They are an abstraction, encapsulation, inheritance,
and polymorphism. Grasping them is key to understanding how Java works.
Basically, Java OOP concepts let us create working methods and variables, then
re-use all or part of them without compromising security.

There are four main OOP concepts in Java. These are:

 Abstraction. Abstraction means using simple things to represent


complexity. We all know how to turn the TV on, but we don’t need to know
how it works in order to enjoy it. In Java, abstraction means simple things
like objects, classes, and variables represent more complex underlying
code and data. This is important because it lets avoid repeating the same
work multiple times.
 Encapsulation. This is the practice of keeping fields within a class private,
then providing access to them via public methods. It’s a protective barrier
that keeps the data and code safe within the class itself. This way, we can
re-use objects like code components or variables without allowing open
access to the data system-wide.
 Inheritance. This is a special feature of Object Oriented Programming in
Java. It lets programmers create new classes that share some of the
attributes of existing classes. This lets us build on previous work without
reinventing the wheel.
 Polymorphism. This Java OOP concept lets programmers use the same
word to mean different things in different contexts. One form of
polymorphism in Java is method overloading. That’s when different
meanings are implied by the code itself. The other form is method
overriding. That’s when the different meanings are implied by the values of
the supplied variables

2.Distinguish between Procedural oriented programming and


Object oriented programming.

PROCEDURAL ORIENTED OBJECT ORIENTED

PROGRAMMING PROGRAMMING

In procedural programming, In object oriented programming,

program is divided into small parts program is divided into small

called functions. parts called objects.

Procedural programming Object oriented programming

follows top down approach. follows bottom up approach.


PROCEDURAL ORIENTED OBJECT ORIENTED

PROGRAMMING PROGRAMMING

Object oriented programming

There is no access specifier in have access specifiers like

procedural programming. private, public, protected etc.

Adding new data and function is Adding new data and function is

not easy. easy.

Procedural programming does not Object oriented programming

have any proper way for hiding provides data hiding so it is more

data so it is less secure. secure.

In procedural programming, Overloading is possible in object

overloading is not possible. oriented programming.

In procedural programming, In object oriented programming,

function is more important than data is more important than

data. function.

Procedural programming is based Object oriented programming is

on unreal world. based on real world.


PROCEDURAL ORIENTED OBJECT ORIENTED

PROGRAMMING PROGRAMMING

Examples: C, FORTRAN, Pascal, Examples: C++, Java, Python, C#

Basic etc. etc.

3.Discuss the advantages of StringBuffer class over String class.


BASIS FOR
STRING STRINGBUFFER
COMPARISON

Basic The length of the String object The length of the StringBuffer can

is fixed. increased.

Modification String object is immutable. StringBuffer object is mutable.

Performance It is slower during It is faster during concatenation.

concatenation.

Memory Consumes more memory. Consumes less memory.

Storage String constant pool. Heap Memory.

4.Develop a java program to demonstrate static variable, static


method.
class StaticDemo
{
public static void copyArg(String str1, String str2)
{
//copies argument 2 to arg1
str2 = str1;
System.out.println("First String arg is: "+str1);
System.out.println("Second String arg is: "+str2);
}
public static void main(String agrs[])
{
/* This statement can also be written like this:
* StaticDemo.copyArg("XYZ", "ABC");
*/
copyArg("XYZ", "ABC");
}
}
Output:

First String arg is: XYZ


Second String arg is: XYZ

STATIC VARIABLE
class VariableDemo
{
static int count=0;
public void increment()
{
count++;
}
public static void main(String args[])
{
VariableDemo obj1=new VariableDemo();
VariableDemo obj2=new VariableDemo();
obj1.increment();
obj2.increment();
System.out.println("Obj1: count is="+obj1.count);
System.out.println("Obj2: count is="+obj2.count);
}
}
Output:

Obj1: count is=2


Obj2: count is=2

5.Discuss the usage of ‘this’ keyword and demonstrate with syntax.

this keyword in java


There can be a lot of usage of java this keyword. In java, this is a reference
variable that refers to the current object.

Usage of java this keyword

Here is given the 6 usage of java this keyword.

1. this can be used to refer current class instance variable.


2. this can be used to invoke current class method (implicitly)
3. this() can be used to invoke current class constructor.
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor call.
6. this can be used to return the current class instance from the method.

EXAMPLE-

public class MyClass {

int x;

// Constructor with a parameter

public MyClass(int x) {

this.x = x;

// Call the constructor

public static void main(String[] args) {

MyClass myObj = new MyClass(5);

System.out.println("Value of x = " + myObj.x);


}

6.Define Inheritance. Explain the different types of inheritance

Inheritance is the process of creating a new Class, called the Derived Class, from
the existing class, called the Base Class. The Inheritance has many advantages,
the most important of them being the reusability of code. Rather than
developing new Objects from scratch, new code can be based on the work of
other developers, adding only the new features that are needed. The reuse of
existing classes saves time and effort.

However, inheritance may be implemented in different combinations in Object-


Oriented Programming languages as illustrated in figure and they include:

 Single Inheritance
 Multi Level Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance
 Multipath inheritance
 Multiple Inheritance

Single Inheritance

When a Derived Class to inherit properties and behavior from a single Base Class,
it is called as single inheritance.

Multi Level Inheritance


A derived class is created from another derived class is called Multi Level
Inheritance

Hierarchical Inheritance

More than one derived class are created from a single base class, is called
Hierarchical Inheritance
Hybrid Inheritance

Any combination of above three inheritance (single, hierarchical and multi level)
is called as hybrid inheritance.

Multipath inheritance

Multiple inheritance is a method of inheritance in which one derived class can


inherit properties of base class in different paths. This inheritance is not
supported in .NET Languages such as C#.

Multiple Inheritance
Multiple inheritances allows programmers to create classes that combine
aspects of multiple classes and their corresponding hierarchies. In .Net
Framework, the classes are only allowed to inherit from a single parent class,
which is called single inheritance. More about.... Why in .NET multiple
inheritance is not allowed

7.Write a Java Program to find sum of elements in a given array.


1. import java.util.Scanner;
2. public class Array_Sum
3. {
4. public static void main(String[] args)
5. {
6. int n, sum = 0;
7. Scanner s = new Scanner(System.in);
8. System.out.print("Enter no. of elements you want in array:");
9. n = s.nextInt();
10. int a[] = new int[n];
11. System.out.println("Enter all the elements:");
12. for(int i = 0; i < n; i++)
13. {
14. a[i] = s.nextInt();
15. sum = sum + a[i];
16. }
17. System.out.println("Sum:"+sum);
18. }
19. }
Output:
$ javac Array_Sum.java
$ java Array_Sum

Enter no. of elements you want in array:5


Enter all the elements:
1
2
3
4
5
Sum:15

9.Define Polymorphism with an Example.


The word polymorphism means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more than
one form. Polymorphism allows us to perform a single action in different ways.
In other words, polymorphism allows you to define one interface and have
multiple implementations

class Animal {

public void animalSound() {

System.out.println("The animal makes a sound");

class Pig extends Animal {

public void animalSound() {

System.out.println("The pig says: wee wee");

}class Dog extends Animal {

public void animalSound() {

System.out.println("The dog says: bow wow");

ETRA INFO FOR QUE

There are two types of polymorphism in java:


1) Static Polymorphism also known as compile time polymorphism
2) Dynamic Polymorphism also known as runtime polymorphism
Compile time Polymorphism (or Static polymorphism)
Polymorphism that is resolved during compiler time is known as static
polymorphism.

Method overloading is an example of compile time polymorphism.

Runtime polymorphism or Dynamic Method Dispatch is a process in


which a call to an overridden method is resolved at runtime rather than
compile-time.
In this process, an overridden method is called through the reference
variable of a superclass. The determination of the method to be called is
based on the object being referred to by the reference variable.

10.Distinguish between multiple inheritance and multilevel


inheritance

“Multiple Inheritance” refers to the concept of one class extending


(Or inherits) more than one base class. The inheritance we learnt
earlier had the concept of one base class or parent. The problem
with “multiple inheritance” is that the derived class will have to
manage the dependency on two base classes.

Multilevel inheritance refers to a mechanism in OO technology


where one can inherit from a derived class, thereby making this
derived class the base class for the new class. As you can see in
below flow diagram C is subclass or child class of B and B is a child
class of A. For more details and example refer – Multilevel
inheritance in Java.
12.Write a simple java program to add two nmbers.
public class MyFirstJavaProgram {

/* This is my first java program.


* This will add two numbers
*/

public static void main(String []args) {


int a = 3, b = 4;
Int sum = a + b;
System.out.println(sum); // prints 7
}
}
Very long questions

1. Define a class Area with two derived classes Square and Rectangle each with a
method with same name findArea() that compute and return the area of rectangle, and
square respectively. Incorporate a main method to invoke and to retrieve the results
of these methods.
answer
refer material

2.Develop a program to perform Bitwise Shift operations (<<, >> and >>>) on suitable data.
Employ the Integer.toBitString() method to display the results.

public class operators {


public static void main(String[] args)
{

int a = 5;
int b = -10;

// left shift operator


// 0000 0101<<2 =0001 0100(20)
// similar to 5*(2^2)
System.out.println("a<<2 = " + (a << 2));

// right shift operator


// 0000 0101 >> 2 =0000 0001(1)
// similar to 5/(2^2)
System.out.println("b>>2 = " + (b >> 2));

// unsigned right shift operator


System.out.println("b>>>2 = " + (b >>> 2));
}
}

Output :
a<<2 = 20
b>>2 = -3
b>>>2 = 1073741821
3. Bring out the memory allocation procedure for non-static and static members of a class
along with the difference in procedure to invoke non-static and static methods. Appreciate the
characteristics of static fields and static methods with suitable examples.

Link:-
https://www.geeksforgeeks.org/difference-between-static-and-non-static-method-in-java/

4. Develop a java program to find the sum and average of n elements. Use a 1D array to
implement the same

// Java program to calculate average of array elements


class GFG {

// Function that return average of an array.


static double average(int a[], int n)
{

// Find sum of array element


int sum = 0;

for (int i = 0; i < n; i++)


sum += a[i];

return sum / n;
}

//driver code
public static void main (String[] args)
{

int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};


int n = arr.length;

System.out.println(average(arr, n));
}
}

Output:
6

5.Discuss the concept of Method overloading? Analyze with an example


Overloading in Java
Overloading allows different methods to have the same name, but different signatures
where the signature can differ by the number of input parameters or type of input
parameters or both. Overloading is related to compile-time (or static) polymorphism.

filter_none
edit
play_arrow
brightness_4
// Java program to demonstrate working of method

// overloading in Java.

public class Sum {

// Overloaded sum(). This sum takes two int parameters

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);

// 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[])

Sum s = new Sum();

System.out.println(s.sum(10, 20));

System.out.println(s.sum(10, 20, 30));

System.out.println(s.sum(10.5, 20.5));

}
Output :
30
60
31.0

6.Develop a java program to search for an element in an array of n elements.


Implement the Linear Search Technique

/ Java code for linearly search x in arr[]. If x


// is present then return its location, otherwise
// return -1
class LinearSearch {
// This function returns index of element x in arr[]
static int search(int arr[], int n, int x)
{
for (int i = 0; i < n; i++) {
// Return the index of the element if the element
// is found
if (arr[i] == x)
return i;
}

// return -1 if the element is not found


return -1;
}

public static void main(String[] args)


{
int[] arr = { 3, 4, 1, 7, 5 };
int n = arr.length;

int x = 4;

int index = search(arr, n, x);


if (index == -1)
System.out.println("Element is not present in the array");
else
System.out.println("Element found at position " + index);
}
}
Output:
Element found at position 1
Long answers
2.Analyze the characteristics of final methods and final classes with suitable examples.

2) final method
A final method cannot be overridden. Which means even though a sub class can
call the final method of parent class without any issues but it cannot override it.

Example:

class XYZ{
final void demo(){
System.out.println("XYZ Class Method");
}
}

class ABC extends XYZ{


void demo(){
System.out.println("ABC Class Method");
}

public static void main(String args[]){


ABC obj= new ABC();
obj.demo();
}
}
The above program would throw a compilation error, however we can use the
parent class final method in sub class without any issues. Lets have a look at this
code: This program would run fine as we are not overriding the final method.
That shows that final methods are inherited but they are not eligible for
overriding.

class XYZ{
final void demo(){
System.out.println("XYZ Class Method");
}
}

class ABC extends XYZ{


public static void main(String args[]){
ABC obj= new ABC();
obj.demo();
}
}
Output:

XYZ Class Method

3) final class
We cannot extend a final class. Consider the below example:

final class XYZ{


}

class ABC extends XYZ{


void demo(){
System.out.println("My Method");
}
public static void main(String args[]){
ABC obj= new ABC();
obj.demo();
}
}
Output:

The type ABC cannot subclass the final class XYZ

3. Compare and Contrast entry controlled and exit controlled loops with a suitable example
for each.

Answer Link:-

https://www.includehelp.com/c-programming-questions/difference-between-
entry-and-exit-controlled-loop.aspx

6. Demonstrate the concept of method overriding with an example.

Answer Link:-

https://www.geeksforgeeks.org/overriding-in-java/

7. Analyze the process of achieving Call by Reference in java with an example.


Answer Link:-

https://www.geeksforgeeks.org/difference-between-call-by-value-and-call-by-
reference/

Access specifiers for classes or interfaces in Java


In Java, methods and data members of a class/interface can have one of the following
four access specifiers. The access specifiers are listed according to their restrictiveness
order.
1) private
2) default (when no access specifier is specified)
3) protected
4) public
But, the classes and interfaces themselves can have only two access specifiers when
declared outside any other class.
1) public
2) default (when no access specifier is specified)

8.Discuss about the various Access specifiers and their significance with an example.

We cannot declare class/interface with private or protected access specifiers. For


example, following program fails in compilation.

filter_none
edit
play_arrow
brightness_4
//filename: Main.java

protected class Test {}

public class Main {

public static void main(String args[]) {

14. Bring out the significance of labelled break and labelled continue with a suitable example
for each.

Links :-

Break

https://www.tutorialspoint.com/java/java_break_statement.htm
continue

https://www.tutorialspoint.com/java/java_continue_statement.htm

You might also like