0% found this document useful (0 votes)
62 views

Test I Model Answer

The document is a class test question paper for the subject JPR (22412) from the Department of Computer Engineering at Government Polytechnic Ambad. It contains two questions with multiple parts each. Question 1 has parts a-f asking to list Java features, write a program to find the greater of two numbers, explain variable length arguments in Java with an example, write a program to display the sum of even numbers from 1 to 20, explain the finalize() method syntax, and explain the four Java access specifiers. Question 2 has parts a-c asking to define a Student class with id and name data members and a method to accept and display data for five students, write a program to swap two numbers using a third

Uploaded by

kira
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Test I Model Answer

The document is a class test question paper for the subject JPR (22412) from the Department of Computer Engineering at Government Polytechnic Ambad. It contains two questions with multiple parts each. Question 1 has parts a-f asking to list Java features, write a program to find the greater of two numbers, explain variable length arguments in Java with an example, write a program to display the sum of even numbers from 1 to 20, explain the finalize() method syntax, and explain the four Java access specifiers. Question 2 has parts a-c asking to define a Student class with id and name data members and a method to accept and display data for five students, write a program to swap two numbers using a third

Uploaded by

kira
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

GOVERNMENT POLYTECHNIC AMBAD

Department Of Computer Engineering

Class Test – I- Model Answer


Subject & Code : JPR ( 22412 ) Max. Marks: 20
Class : CO4I Time : 11 AM to 12 PM
Date : 04/04/2022 Academic Year 2021-2022

Instructions – i. Illustrate your answers with neat sketches whenever necessary.


iii. Assume suitable data if necessary.
Sr.
Questions Marks
No.

Q 1. Attempt any Four of the following. [2*4=08M]


a) List and explain any four features of Java. 2M
(Any 4
Ans. 1. Compile & Interpreted: Java is a two staged system. It combines both
features :
approaches. First java compiler translates source code into byte code instruction.
1/2 mark
Byte codes are not machine instructions. In the second stage java interpreter
each)
generates machine code that can be directly executed by machine. Thus java is
both compile and interpreted language.
2. Platform independent and portable: Java programs are portable i.e. it can be
easily moved from one computer system to another. Changes in OS, Processor,
system resources won‟t force any change in java programs. Java compiler
generates byte code instructions that can be implemented on any machine as well
as the size of primitive data type is machine independent.
3. Object Oriented: Almost everything in java is in the form of object. All
program codes and data reside within objects and classes. Similar to other OOP
languages java also has basic OOP properties such as encapsulation,
polymorphism, data abstraction, inheritance etc. Java comes with an extensive set
of classes (default) in packages.
4. Robust & Secure: Java is a robust in the sense that it provides many safeguards
to ensure reliable codes. Java incorporates concept of exception handling which
captures errors and eliminates any risk of crashing the system. Java system not
only verifies all memory access but also ensure that no viruses are communicated
with an applet. It does not use pointers by which you can gain access to memory
locations without proper authorization.
5. Distributed: It is designed as a distributed language for creating applications on
network. It has ability to share both data and program. Java application can open
and access remote object on internet as easily as they can do in local system.
6. Multithreaded: It can handle multiple tasks simultaneously. Java makes this
possible with the feature of multithreading. This means that we need not wait for
the application to finish one task before beginning other.
7. Dynamic and Extensible: Java is capable of dynamically linking new class
library‟s method and object. Java program supports function written in other
languages such as C, C++ which are called as native methods. Native methods are
linked dynamically at run time.
Write a program to find greater number among two numbers.
b) 2M
import java.util.*;
Ans. class greater correct logic
{ and syntax
public static void main(String args[]) 2M
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any two numbers :”);
a=sc.nextInt( );
b=sc.nextInt( );
if (a>b)
System.out.println("Greater number between "+a+ " and "+b +" is = "+a);
else
System.out.println("Greater number between "+a+ " and "+b +" is = "+b);
}
}
Output:
E:\java\bin>javac greater.java
E:\java\bin>java greater
Enter any two numbers 100 150
Greater number between 100 and 150 is = 150

c) Explain variable length arguments in java with example. 2M

Ans. A method with variable length arguments (varargs) in Java can have zero or Explanation
multiple arguments. Variable length arguments are most useful when the number with syntax:
of arguments to be passed to the method is not known beforehand. They also 1M
reduce the code as overloaded methods are not required. Example:1M

Syntax of varargs:
The varargs uses ellipsis i.e. three dots after the data type. Syntax is as follows:
return_type method_name(data_type... variableName){}

A program that demonstrates this is given as follows:


class ExVarargs
{
static void display(String... values)
{
System.out.println("display method invoked ");
for(String s:values)
{
System.out.println(s);
}
}

public static void main(String args[])


{
display();//zero argument
display("my","name","is","varargs");//four arguments
}
}

d) Write a java program to display sum of even numbers between1 to 20. 2M


Ans. public class Sum Correct
{ Logic and
public static void main(String[] args) syntax: 2M
{
int sum=0;
for (int i=0; i<=20;i=i+2)
{
sum = sum+i;
}
System.out.println("Sum of these numbers: "+sum);
}
}
Output:
Sum of even numbers: 110

e) State use of finalize( ) method with its syntax. 2M

Ans. Use of finalize( ): Explanation


Sometimes an object will need to perform some action when it is destroyed. Eg. If and syntax:
an object holding memory, then before the object is garbage collected this memory 2M
should be freed. To handle such situations, java provide a mechanism called
finalization. In finalization, specific actions that are to be done when an object is
garbage collected can be defined. To add finalizer to a class define the finalize()
method. The java run-time calls this method whenever it is about to recycle an
object.

Method used for Garbage Collection finalize:


The java.lang.Object.finalize() is called by garbage collector on an object when
garbage collection determines that there are no more reference to the object.
A subclass override the finalize method to dispose of system resources or to
perform other cleanup.
Inside the finalize() method, the actions that are to be performed before an object
is to be destroyed, can be defined. Before an object is freed, the java run-time calls
the finalize() method on the object.
Syntax :
protected void finalize()
{ // finalization code here
}

f) Explain the four access specifiers in Java. 2M

Ans. There are 4 types of java access modifiers/specifiers: four access


1. private 2. default 3. Protected 4. public specifiers:
1) private access modifier: The private access modifier is accessible only within 1/2M Each
class.
2) default access specifier: If you don’t specify any access control specifier, it is
default, i.e. it becomes implicit public and it is accessible within the program.
3) protected access specifier: The protected access specifier is accessible within
package and outside the package but through inheritance only.
4) public access specifier: The public access specifier is accessible everywhere. It
has the widest scope among all other modifiers.

Q 2. Attempt any Three of the following. [4*3=12M]

a) Define a class student with int id and string name as data members and a 4M
method void SetData ( ). Accept and display the data for five students.
Ans. import java.io.*; Correct
class student syntax and
{ logic: 4M
int id;
String name;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
void SetData()
{
System.out.println("enter id and name for student");
id=Integer.parseInt(br.readLine());
name=br.readLine();
}
void display()
{
System.out.println("The id is " + id + " and the name is "+ name);
}
public static void main(String are[])
{
student[] arr;
arr = new student[5];
int i;
for(i=0;i<5;i++)
{
arr[i] = new student();
}
for(i=0;i<5;i++)
{
arr[i].SetData();
}
for(i=0;i<5;i++)
{
arr[i].display();
}
}
}

b) 4M
Explain the command line arguments. Write a program to accept two
numbers from command line and print sum of these two numbers.

Java Command Line Argument:


The java command-line argument is an argument i.e. passed at the time of running Explanation:
the java program. The arguments passed from the console can be received in the 2M
java program and it can be used as an input. So, it provides a convenient way to Program
check the behavior of the program for the different values. You can pass N (1,2,3 with correct
and so on) numbers of arguments from the command prompt. There is no logic and
restriction on the number of java command line arguments. You can specify any syntax: 2M
number of arguments. Information is passed as Strings. They are captured into the
String args of your main method.

Program of command-line argument in java.


class addition
{
public static void main(String args[])
{
int a,b;
a= Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
int c= a+b;
System.out.println("Addition= "+c);
}
}

c) Define a class circle having data members pi and radius. Initialize and display 4M
values of data members also calculate area of circle and display it.

Ans. class abc Correct logic


{ and syntax:
float pi, radius; 4M
abc(float p, float r)
{
pi=p;
radius=r;
}
void area()
{
float ar=pi*radius*radius;
System.out.println("Area="+ar);
}
void display()
{
System.out.println("Pi="+pi);
System.out.println("Radius="+radius);
}
}
class area
{
public static void main(String args[])
{
abc a=new abc(3.14f,5.0f);
a.display();
a.area();
}
}

d) Illustrate with example the use of switch case statement. 4M

Ans. Switch case statement: Explana


The switch statement executes one statement from multiple conditions. It is like if- tion 2M
else-if ladder statement. The switch statement works with byte, short, int, long,
enum types, String and some wrapper types like Byte, Short, Int, and Long. In Example
other words, the switch statement tests the equality of a variable against multiple 2M
values.

1. There can be one or N number of case values for a switch expression.


2. The case value must be of switch expression type only. The case value must be
literal or constant. It doesn't allow variables.
3. The case values must be unique. In case of duplicate value, it renders compile-
time error.
4. The Java switch expression must be of byte, short, int, long (with its Wrapper
type), enums and string.
5. Each case statement can have a break statement which is optional. When
control reaches to the break statement, it jumps the control after the switch
expression. If a break statement is not found, it executes the next case.
6. The case value can have a default label which is optional.

Syntax:
switch(expression)
{
case value1:
block 1;
break;
case value2:
block 2;
break;
.
.
.
default:
default block;
break;
}

Example:
public class SwitchExample
{
public static void main(String[] args) {
int number=20;
switch(number){
case 10: System.out.println("You are in 10");break;
case 20: System.out.println("You are in 20");break;
case 30: System.out.println("You are in 30");break;
default:System.out.println("Not in 10, 20 or 30");
}
}
}

You might also like