Java Worksheet
Q1.
What is meant by a package? Name any two Java Application Programming Interface packages.
[2]
Ans) A package is a small amount of data sent over a network. Two Java Application Programming Interface
packages are the official Java API and JDK or oracle.
Q2.
Find the errors in the given program segment and re-write the statements correctly to assign values to an
integer array. [2]
view source
print?
1 int a = new int( 5 );
2 for( int i=0; i<=5; i++ ) a[i]=i;
Ans) int [a]= int[5]
Q3.
Identify the statements listed below as assignment, increment, method invocation or object creation statements.
[2]
(i) System.out.println(“Java”); Method Invocation
(ii) costPrice = 457.50; assignment
(iii) Car hybrid = new Car(); Object Creation
(iv) petrolPrice++; Increment
Q4.
State the Java concept that is implemented through:
i) a super class and a subclass.
ii) the act of representing essential features without including background details. [2]
Ans)
i) Inheritance
ii) Abstraction
Q5.
State the output of the following program segment: [2]
1 String str1 = "great"; String str2 = "minds";
2 System.out.println(strl.substring(0,2).concat(str2.substring(l)));
3 System.out.println(("WH" + (strl.substring(2).toUpperCase())));
Ans) WHEAT
Q6.
What is an exception? [2]
Ans) Exceptions are the events that occur during the execution of programs that disrupt
the normal flow of instructions.
Q7.
Write Java statement to create an object mp4 of class digital. [2]
Ans) Digital mp4= new digital()
Q8.
How many times will the following loop execute? What value will be returned? [2]
int x = 2, y = 50;
do{
++x;
y-=x++;
}while(x<=10);
return y;
Ans) 5,15
Q9. [5]
Define a class named movieMagic with the following description:
Instance variables/data members:
int year – to store the year of release of a movie
String title – to store the title of the movie.
float rating – to store the popularity rating of the movie.
(minimum rating = 0.0 and maximum rating = 5.0)
Member Methods:
(i) movieMagic() Default constructor to initialize numeric data members to 0 and String data member to “”.
(ii) void accept() To input and store year, title and rating.
(iii) void display() To display the title of a movie and a message based on the rating as per the table below.
RATING MESSAGE TO BE DISPLAYED
0.0 to 2.0 Flop
2.1 to 3.4 Semi-hit
3.5 to 4.5 Hit
4.6 to 5.0 Super Hit
Write a main method to create an object of the class and call the above member methods.
Ans) class movieMagic
{
int year;
String title;
float rating;
movieMagic()
{
year = 0;
rating = 0.0;
title = "";
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the title of the movie : ");
title = sc.nextLine();
System.out.print("Enter the year of its release : ");
year = sc.nextInt();
System.out.print("Enter the movie rating : ");
rating = sc.nextFloat();
}
void display()
{
System.out.println("The title of the movie is : "+title);
if( rating >= 0.0 && rating <= 2.0 )
{
System.out.println("The movie was a Flop");
}
else if( rating >= 2.1 && rating <= 3.4 )
{
System.out.println("The movie was a Semi-hit");
}
else if( rating >= 3.5 && rating <= 4.5 )
{
System.out.println("The movie was a Hit");
}
else if ( rating >= 4.6 && rating <= 5.0 )
{
System.out.println("The movie was a Super Hit");
}
else
{
System.out.println("Rating should be between 0.0 and 5.0");
}
}
public static void main(String args[])
{
movieMagic ac= new movieMagic();
ac.accept();
ac.display();
}
Q10. [4]
A special two-digit number is such that when the sum of its digits is added to the product of its digits, the
result is equal to the original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of its digits = 5 x 9 = 45
Sum of the sum of digits and product of digits= 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value
is equal to the number input, output the message “Special 2-digit number” otherwise, output the message “Not
a Special 2-digit number”.
Ans)
Import java.io.*;
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.print("Enter a 2 digit number: ");
int n = Integer.parseInt(br.readLine());
int first, last, sum, pro;
{
first = n/10;
last = n%10;
sum = first + last;
pro = first * last;
if((sum + pro) == n)
{
System.out.println("Output : The number "+n+" is a Special Two-Digit Number.");
}
else
{
System.out.println("Output : The number is Not a Special Two-Digit Number.");}}}}