1.
Prove the statement “The credit of Java’s global acceptance
goes to the Bytecode.”3
➔
2. Demonstrate the process of writing, compiling and executing a
Java Program [program -1M, Compilation-1M, Execution-1M )3
➔
3. Describe the following buzzwords of Java (Any Two)(i) Secure
(ii) Robust (iii) Portable (iv) Architecture Neutral (v) Interpreted4
➔
As described earlier, Java enables the creation of cross-platform
programs by compiling into an intermediate representation called
Java bytecode. This code can be executed on any system that
implements the Java Virtual Machine. Most previous attempts at
cross-platform solutions have done so at the expense of
performance. As explained earlier, the Java bytecode was carefully
designed so that it would be easy to translate directly into native
machine code for very high performance by using a just-in-time
compiler. Java run-time systems that provide this feature lose none of
the benefits of the platform-independent code.
4. State the role of JVM and JRE in Java.3
➔
5. Describe any 3 primitive data types of Java with suitable
examples. [each data type -1M]3
➔
6. Describe Explicit type casting in Java with an example
program.[concept -1M, program-2M]3
➔
7. Explain the following OOP concepts with suitable examples:
(Any Two) (i) Encapsulation (ii) abstraction (iii) Polymorphism
(iv) Inheritance (v) class (vi) object (vii) reference[Explanation-
1M, Example -1M]4
➔ Encapsulation- Encapsulation is the mechanism that binds together code and the
data it manipulates, and keeps both safe from outside interference and misuse. One
way to think about encapsulation is as a protective wrapper that prevents the code
and data from being arbitrarily accessed by other code defined outside the wrapper.
Access to the code and data inside the wrapper is tightly controlled through a well-
defined interface. To relate this to the real world, consider the automatic transmission
on an automobile. It encapsulates hundreds of bits of information about your engine,
such as how much you are accelerating, the pitch of the surface you are on, and the
position of the shift lever. You, as the user, have only one method of affecting this
complex encapsulation: by moving the gear-shift lever. You can’t affect the
transmission by using the turn signal or windshield wipers, for example. Thus, the
gear-shift lever is a well-defined (indeed, unique) interface to the transmission.
Abstraction- An essential element of object-oriented programming is
abstraction. Humans manage complexity through abstraction. For example,
people do not think of a car as a set of tens of thousands of individual parts.
They think of it as a well-defined object with its own unique behavior. This
abstraction allows people to use a car to drive to the grocery store without
being overwhelmed by the complexity of the individual parts. They can ignore
the details of how the engine, transmission, and braking systems work.
Instead, they are free to utilize the object as a whole.
Polymorphism- Polymorphism (from Greek, meaning “many forms”) is a
feature that allows one interface to be used for a general class of actions. The
specific action is determined by the exact nature of the situation. Consider a
stack (which is a last-in, first-out list). You might have a program that requires
three types of stacks. One stack is used for integer values, one for floating-
point values, and one for characters. The algorithm that implements each
stack is the same, even though the data being stored differs. In a non–object-
oriented language, you would be required to create three different sets of
stack routines, with each set using different names. However, because of
polymorphism, in Java you can specify a general set of stack routines that all
share the same names.
Inheritance- Inheritance is the process by which one object acquires the
properties of another object. This is important because it supports the concept
of hierarchical classification. As mentioned earlier, most knowledge is made
manageable by hierarchical (that is, top-down) classifications. For example, a
Golden Retriever is part of the classification dog, which in turn is part of the
mammal class, which is under the larger class animal. Without the use of
hierarchies, each object would need to define all of its characteristics
explicitly. However, by use of inheritance, an object need only define those
qualities that make it unique within its class. It can inherit its general attributes
from its parent. Thus, it is the inheritance mechanism that makes it possible
for one object to be a specific instance of a more general case.
Class-The class is at the core of Java. It is the logical construct upon which
the entire Java language is built because it defines the shape and nature of
an object. As such, the class forms the basis for object-oriented programming
in Java. Any concept you wish to implement in a Java program must be
encapsulated within a class.
Object- An entity that has state and behavior is known as an object e.g., chair, bike,
marker, pen, table, car, etc. It can be physical or logical (tangible and intangible). The
example of an intangible object is the banking system.
An object has three characteristics:
State: represents the data (value) of an object.
Behavior: represents the behavior (functionality) of an object such as deposit, withdraw,
etc.
Identity: An object identity is typically implemented via a unique ID. The value of the ID
is not visible to the external user. However, it is used internally by the JVM to identify
each object uniquely.
Reference- A reference type is a data type that’s based on a class rather than on one of
the primitive types that are built in to the Java language. The class can be a class that’s
provided as part of the Java API class library or a class that you write yourself.
Either way, when you create an object from a class, Java allocates the amount of
memory the object requires to store the object. Then, if you assign the object to a
variable, the variable is actually assigned a reference to the object, not the object itself.
This reference is the address of the memory location where the object is stored.
To declare a variable using a reference type, you simply list the class name as the data
type. For example, the following statement defines a variable that can reference objects
created from a class named Ball:
Ball b;
8 Explain the working of following operators with suitable
examples: (Any Two) [ Explanation –1M and Example -
1M](i) Short circuit operator (ii) bitwise operators (iii)
Logical operators (iv) conditional operator3
➔
Logical \/
The conditional operator is also known as the ternary operator. This operator consists
of three operands and is used to evaluate Boolean expressions. The goal of the operator
is to decide; which value should be assigned to the variable. The operator is written as:
variable x = (expression)? value if true: value if false
Example
public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
[Link]("Value of b is: " + b);
b = (a == 10) ? 20: 30;
[Link](“Value of b is: " + b);
}
}
Output
Value of b is: 30
Value of b is: 20
9 Explain the working of the following String methods
with suitable example snippets (Any Two)[working-0.5M,
example –1.5M](i) length() (ii) indexOf() (iii) lastIndexOf()
(iv) charAt() (v) substring() (vi) toCharArray() 4
➔
Example:
class chararray
{
public static void main(String args[])
{
String s=”Hello”;
char[] ch= [Link]();
for (int i = 0; i < [Link]; i++)
{
[Link](ch[i]);
}
}
}
Output:
H
e
l
l
o
10 Explain the concept of Method overloading / Compile time
Polymorphism with appropriate example program [
Concept -1M, Program -2/3M]3/4
➔
11 Describe the importance of ‘this’ keyword in Java. Give3
➔
12 Write down the syntax for compile time initialization of
1Dand 2D arrays with all possibilities.[1D array =1M, 2D
array=2M]3
➔ 1d array \/
You can also construct an array by initializing the array at declaration time. If you
are initializing the array it is another way to construct an array.
dataType arrayName[] = {value1, value2, …valueN}
Some point about Initialization:
• If you choose datatype of an array is int then values should be int type. It
means a type of value is totally dependent on datatype of an array.
• Value1 will be store at 0 indexes, value2 in 1 index and so on.
• The size of the array depends upon how many values you are providing at
the time of initialization.
int number[] = {11, 22, 33 44, 55, 66, 77, 88, 99, 100}
It’s created an array of int type as you see we are initializing it during declaration.
To initialize the Array we have to put the values at each index of array.
Output: 11
22
33
44
55
66
77
88
99
100
Two – dimensional array is the simplest form of a multidimensional array. A two
– dimensional array can be seen as an array of one – dimensional array for
easier understanding.
Indirect Method of Declaration:
• Declaration – Syntax:
• data_type[][] array_name = new data_type[x][y];
• For example: int[][] arr = new int[10][20];
• Initialization – Syntax:
• array_name[row_index][column_index] = value;
• For example: arr[0][0] = 1;
Example:
class GFG {
public static void main(String[] args)
int[][] arr = new int[10][20];
arr[0][0] = 1;
[Link]("arr[0][0] = " + arr[0][0]);
Output:
arr[0][0] = 1
Direct Method of Declaration:
Syntax:
data_type[][] array_name = {
{valueR1C1, valueR1C2, ....},
{valueR2C1, valueR2C2, ....}
};
For example: int[][] arr = {{1, 2}, {3, 4}};
Example:
class GFG {
public static void main(String[] args)
int[][] arr = { { 1, 2 }, { 3, 4 } };
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
[Link]("arr[" + i + "][" + j + "] = "
+ arr[i][j]);
Output:
arr[0][0] = 1
arr[0][1] = 2
arr[1][0] = 3
arr[1][1] = 4
13 Justify the statement “Java doesn’t support destructors”.3
➔ In Java, the garbage collector automatically deletes the unused
objects to free up the memory. Developers have no need to mark
the objects for deletion, which is error-prone and vulnerable to the
memory leak
14 Explain the concept of pass-by-reference with suitable
example program. [concept-1M, program-2M]3
➔ Java provides only call-by-value, not call by reference. If we call a method passing a
value, the changes being done in the called method is not affected in the calling
method. Array of object stores as elements of an array. Array of objects is not the
object itself that is stored in an array but the reference of the object is stored. An
array of objects is created using the object class.
15 Write a program to demonstrate passing object as a
parameter to the method and returning object from a
method. Describe in brief [Passing Object -1.5/2M, Returning Object –
1.5/2M]3/4
➔ import [Link].*;
class complex
{
float r,i;
public complex(){}
public complex(float rl,float img)
{
r=rl;
i=img;
}
void show()
{
[Link](r+ "+i "+i);
}
complex add(complex B)
{
complex T=new complex();
T.r=r+B.r;
T.i=i+B.i;
return (T);
}
}
class prg45complex
{
public static void main(String args[])
{
complex A=new complex(31.3f,27.5f);
complex B=new complex(3.7f,4.5f);
complex C=new complex();
[Link]();
[Link]();
C=[Link](B);
[Link]("Addition=");
[Link]();
}
}
16 Explain the concept of Method overriding with appropriate
example program [ Concept -1M, Program -2/3M]3/4
➔
17 State the significance of static method. Write one example
program for the same. [concept-1M, program-2M]3
➔
18 Explain “one interface multiple methods” approach of Java
with appropriate real world program. [concept-1M, program-3M]4
➔ Interface is similar to abstract class and it contains the declaration of the methods
without the body. Implicitly all members of interface is public. More than one class
can implement same interface and it should implement all methods in the interface.
So, any object of class can be used and thus it supports polymorphism concepts of
object oriented principle by having multiple methods with one interface.
class MammalInt implements Animal {
public void eat() {
[Link]("Mammal eats");
}
public void travel() {
[Link]("Mammal travels");
}
public int noOfLegs() {
return 0;
}
public static void main(String args[]) {
MammalInt m = new MammalInt();
[Link]();
[Link]();
}
}
This will produce the following result −
Output
Mammal eats
Mammal travels
19 State 3 uses of final keyword with code snippets. 3(269,329)
➔ The keyword final has three uses. First, it can be used to create the equivalent of a named
constant.
Second :
20 One program on the basis of array of objects. (Practice
Examples: Employee, BookStore, Product)6
➔ class Product
{
int pro_Id;
String pro_name;
//Product class constructor
Product(int pid, String n)
{
pro_Id = pid;
pro_name = n;
}
public void display()
{
[Link]("Product Id = "+pro_Id + " " + " Product Name = "+pro_na
me);
[Link]();
}
}
class ArrayOfObjects
{
public static void main(String args[])
{
//create an array of product object
Product[] obj = new Product[5] ;
//create & initialize actual product objects using constructor
obj[0] = new Product(23907,"Dell Laptop");
obj[1] = new Product(91240,"HP 630");
obj[2] = new Product(29823,"LG OLED TV");
obj[3] = new Product(11908,"MI Note Pro Max 9");
obj[4] = new Product(43590,"Kingston USB");
//display the product object data
[Link]("Product Object 1:");
obj[0].display();
[Link]("Product Object 2:");
obj[1].display();
[Link]("Product Object 3:");
obj[2].display();
[Link]("Product Object 4:");
obj[3].display();
[Link]("Product Object 5:");
obj[4].display();
}
}
21 One program on the basis of Inheritance. 6
➔