Class 10 Java Paper 2017
Class 10 Java Paper 2017
Section A
Question 1
Answer
Inheritance is the mechanism by which a class acquires the properties and methods of another
class.
1. <
2. ++
3. &&
4. ?:
Answer
(c) State the number of bytes occupied by char and int data types.
Answer
Answer
1. System.out.println(x[1]);
2. System.out.println(x[3].length());
Answer
Answer
1. import
2. Array
Answer
(c) State the data type and value of res after the following is executed:
char ch='t';
res= Character.toUpperCase(ch);
Answer
(d) Give the output of the following program segment and also mention the number of
times the loop is executed:
int a,b;
for (a = 6, b = 4; a <= 24; a = a + 6)
{
if (a%b == 0)
break;
}
System.out.println(a);
Answer
Explanation
a b Remarks
6 4 1st Iteration
12 4 2nd Iteration
In 2nd iteration, as a%b becomes 0 so break statement is executed and the loop exits.
Program control comes to the println statement which prints the output as current value of a
which is 12.
char ch = 'F';
int m = ch;
m=m+5;
System.out.println(m + " " + ch);
Answer
75 F
Explanation
The statement int m = ch; assigns the ASCII value of F (which is 70) to variable m. Adding 5
to m makes it 75. In the println statement, the current value of m which is 75 is printed followed
by space and then value of ch which is F.
Question 3
Answer
a * Math.pow(x, 5) + b * Math.pow(x, 3) + c
Answer
Answer
A class can create objects of itself with different characteristics and common behaviour. So, we
can say that an Object represents a specific state of the class. For these reasons, an Object is
called an Instance of a Class.
int i = 1;
int d = 5;
do {
d=d*2;
System.out.println(d);
i++ ; } while ( i<=5);
Answer
int i = 1;
int d = 5;
for (i = 1; i <= 5; i++) {
d=d*2;
System.out.println(d);
}
(e) Differentiate between constructor and function.
Answer
Constructor Function
Constructor is a block of code that Function is a group of statements that can be called at any point
initializes a newly created object. in the program using its name to perform a specific task.
0
Today i Holiday
Explanation
1. r1 has 5.83
2. r2 has 4.0
Explanation
1. Math.min(-2.83, -5.83) returns -5.83 as -5.83 is less than -2.83. (Note that these are
negative numbers). Math.abs(-5.83) returns 5.83.
2. Math.floor(16.3) returns 16.0. Math.sqrt(16.0) gives square root of 16.0 which is 4.0.
Result 1 = 26100200
Result 2 = 126
Explanation
1. As A and B are strings so String D=A+B+"200"; joins A, B and "200" storing the string
"26100200" in D.
2. Integer.parseInt() converts strings A and B into their respective numeric values. Thus, x
becomes 26 and y becomes 100.
3. As Java is case-sensitive so D and d are treated as two different variables.
4. Sum of x and y is assigned to d. Thus, d gets a value of 126.
(i) Analyze the given program segment and answer the following questions:
for(int i=3;i<=4;i++ ) {
for(int j=2;j<i;j++ ) {
System.out.print("" ); }
System.out.println("WIN" ); }
Answer
(j) What is the difference between the Scanner class functions next() and nextLine()?
Answer
next() nextLine()
It reads the input only till space so it can read It reads the input till the end of line so it can read a full
only a single word. sentence including spaces.
Section B
Question 4
class : ElectricBill
Member methods:
void accept( ) — to accept the name of the customer and number of units consumed
void calculate( ) — to calculate the bill as per the following tariff:
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
Write a main method to create an object of the class and call the above member methods.
Answer
import java.util.Scanner;
Output
Question 5
Write a program to accept a number and check and display whether it is a spy number or not.
(A number is spy if the sum of its digits equals the product of its digits.)
Answer
import java.util.Scanner;
sum += digit;
prod *= digit;
num /= 10;
}
if (sum == prod)
System.out.println(orgNum + " is Spy Number");
else
System.out.println(orgNum + " is not Spy Number");
}
}
Output
Question 6
Using switch statement, write a menu driven program for the following:
1. To find and display the sum of the series given below:
S = x1 - x2 + x3 - x4 + x5 .......... - x20
(where x = 2)
2. To display the following series:
1 11 111 1111 11111
Answer
import java.util.Scanner;
switch (choice) {
case 1:
int sum = 0;
for (int i = 1; i <= 20; i++) {
int x = 2;
int term = (int)Math.pow(x, i);
if (i % 2 == 0)
sum -= term;
else
sum += term;
}
System.out.println("Sum=" + sum);
break;
case 2:
int term = 1;
for (int i = 1; i <= 5; i++) {
System.out.print(term + " ");
term = term * 10 + 1;
}
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Output
Question 7
Write a program to input integer elements into an array of size 20 and perform the following
operations:
Answer
import java.util.Scanner;
sum += arr[i];
}
Output
Question 8
1. void check (String str , char ch ) — to find and print the frequency of a character in a
string.
Example:
Input:
str = "success"
ch = 's'
Output:
number of s present is = 3
2. void check(String s1) — to display only vowels from string s1, after converting it to
lower case.
Example:
Input:
s1 ="computer"
Output : o u e
Answer
Output
Question 9
Write a program to input forty words in an array. Arrange these words in descending order of
alphabets, using selection sort technique. Print the sorted array.
Answer
import java.util.Scanner;
System.out.println("Sorted Names");
for (int i = 0; i < n; i++) {
System.out.println(a[i]);
}
}
}
Output