Java Worksheet: Ans) Int (A) Int (5) Q3
Java Worksheet: Ans) Int (A) Int (5) Q3
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;
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.
Ans)
i) Inheritance
ii) Abstraction
Q5.
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.
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.
Write a main method to create an object of the class and call the above member methods.
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.*;