Section A
Question 1(i)
What will be the output of the following code?
int size = 2;
if (size < 0)
System.out.println("Small");
else if (size == 0)
System.out.println("Medium");
else
System.out.println("Large");
- Small
- Large
- Medium
- Runtime error
Answer
Large
Reason — Since size > 0, hence the conditions of if and else if are false. Thus, the else block will be executed and "Large" will be printed on the screen.
Question 1(ii)
Which of the following statements involves a fall through?
- if-else
- for loop
- if-else-if
- switch
Answer
switch
Reason — Omitting break statement leads to program execution continuing into the next case and onwards till a break statement is encountered or end of switch is reached.
Question 1(iii)
Which of the following statement is true for logical errors?
- The compiler does not detect these errors.
- There is no indication of error when the program is executed.
- The program may produce correct results for some input data and wrong results for other input data.
- All of the above
Answer
All of the above
Reason — The compiler does not detect logical errors and there is no indication of error when the program is executed. The program may produce correct results for some input data and wrong results for other input data.
Question 1(iv)
Object oriented programming mainly uses
- top down approach
- top down and bottom up approach
- bottom up approach
- None of the above
Answer
bottom up approach
Reason — Object Oriented Programming follows bottom-up approach because in OOP we first identify the smallest parts of the program i.e. the objects. We then combine these objects to develop the complete program.
Question 1(v)
In which technique are the values of actual parameters copied to the formal parameters?
- Call by reference
- Call by value
- Call by argument
- Call by method
Answer
Call by value
Reason — In call by value, the actual parameters are copied to formal parameters. Any changes to formal parameters are not reflected onto the actual parameters.
Question 1(vi)
Which package would you import for the Scanner class?
- java.util.*;
- java.awt.*;
- java.io.*;
- java.lang.*;
Answer
java.util.*;
Reason — Scanner class is a part of java.util package.
Question 1(vii)
Give the output of Math.abs(x);
when x = -9.99.
- -9.99
- 9.99
- 0.99
- None of these
Answer
9.99
Reason — Math.abs() returns the absolute value of its argument. Its return type is same as the type of its argument. Thus, Math.abs(-9.99) returns 9.99.
Question 1(viii)
A/An ............... is an abstract description of a set of objects.
- abstraction
- encapsulation
- polymorphism
- class
Answer
class
Reason — A class is an abstract description of a set of objects.
Question 1(ix)
This variable can be accessed by calling with the class name.
- Instance
- Local
- Class
- None of these
Answer
Class
Reason — Class variable can be accessed by calling with the class name.
Question 1(x)
The main objective of passing arguments to functions is
- message passing
- parameter passing
- variable passing
- argument passing
Answer
message passing
Reason — The main objective of passing arguments to functions is message passing.
Question 1(xi)
If int arr [] = {3, 5, 7, 9}; what is the value of arr.length?
- 3
- 5
- 4
- Cannot be determined
Answer
4
Reason — length property stores the number of elements in an array. Thus, the value of arr.length is 4 as there are 4 elements in the array.
Question 1(xii)
A binary search
- can be used with sorted arrays only.
- can be used with unsorted arrays only.
- can be used with both sorted and unsorted arrays.
- cannot be used with arrays.
Answer
can be used with sorted arrays only.
Reason — A binary search can be used with sorted arrays only.
Question 1(xiii)
Corresponding wrapper class of float data type is
- FLOAT
- float
- Float
- Floating
Answer
Float
Reason — Corresponding wrapper class of float data type is Float.
Question 1(xiv)
What will be the output of the following code?
System.out.println("Lucknow".substring (0, 4));
- Lucknow
- Luckn
- Luck
- luck
Answer
Luck
Reason — The substring() method returns a substring beginning from the startindex and extending to the character at index endIndex - 1. Since a string index begins at 0, the character at startindex (0) is 'L' and the character at the endIndex (4-1 = 3) is 'k'. Thus, "Luck" is extracted and printed on the screen.
Question 1(xv)
This access specifier is the most open access level.
- Public
- Protected
- Private
- Default
Answer
Public
Reason — A data member or member method declared as public is accessible inside as well as outside of the class in which it is declared.
Question 1(xvi)
The method to determine whether the specified char value is in uppercase or not.
- toUpperCase(char)
- toLowerCase(char)
- isLowerCase(char)
- isUpperCase(char)
Answer
isUpperCase(char)
Reason — isUpperCase() is used to check if the character given as its argument is in upper case or not. It returns true if the argument is in uppercase, else returns false.
Question 1(xvii)
Assertion (A) Method should be called explicitly either with object reference or class reference.
Reason (R) Method can be any user defined name.
- Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A).
- Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A).
- Assertion (A) is true and Reason (R) is false.
- Assertion (A) is false and Reason (R) is true.
Answer
Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A).
Reason — Assertion (A) is true as a method should be called explicitly either with object reference or class reference. Reason (R) is also true as a method can be any user defined name, except a keyword. Reason (R) is not the correct explanation of Assertion (A).
Question 1(xviii)
Read the following text and choose the correct answer:
Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.
Which of these access specifier must be used for class so that it can be inherited by another subclass?
- public
- private
- protected
- none of the mentioned
Answer
public
Reason — According to the given context, public access specifier can be used for class so that it can be inherited by another subclass.
Question 1(xix)
Assertion (A) The return statement causes program control to transfer back to the caller of the method.
Reason (R) The return statement immediately terminates the method in which it is executed.
- Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A).
- Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A).
- Assertion (A) is true and Reason (R) is false.
- Assertion (A) is false and Reason (R) is true
Answer
Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A).
Reason — Assertion (A) is true as the return statement causes program control to transfer back to the caller of the method. Reason (R) is also true as the return statement immediately terminates the method in which it is executed and the control transfers back to the caller method. Thus, Reason (R) correctly explains Assertion (A).
Question 1(xx)
Correct statement to declare an integer array of 10 elements.
- int[ ] arr = new int[10];
- int arr;
- int arr (10);
- int ( ) arr = new int (10);
Answer
int[ ] arr = new int[10];
Reason — The correct syntax to declare an array is as follows:
datatype[ ] array_name = new datatype[size];
The statement int[ ] arr = new int[10];
follows the correct syntax of array declaration.
Question 2(i)
Write the values of r and s after the execution of the following code.
int p = 11, q = 21, r, s;
r = ++q;
s = p++;
r++;
Answer
After execution of the given code, r = 23 and s = 11.
Explanation
Statement | Remarks |
---|---|
int p = 11, q = 21, r, s; | p = 11, q = 21, r and s are declared |
r = ++q; | r = 22 (prefix operator first increments the value and then uses it) |
s = p++; | s = 11 (postfix operator first uses the value and then increments it) |
r++; | r = 23 (value of r is incremented by 1) |
Question 2(ii)
Observe the following code and find that how many times will the loop execute?
int sum = 0, score = 0;
double t;
do
{
score = score + 1;
sum = sum + score;
} while(score <= 3);
t = sum / 3;
Answer
The loop will execute 4 times.
Explanation
Iteration | score | sum | Remarks |
---|---|---|---|
0 | 0 | score is declared as 0 | |
1 | 1 | 1 | score = 0 + 1 = 1 sum = 0 + 1 = 1 |
2 | 2 | 3 | score = 1 + 1 = 2 sum = 1 + 2 = 3 |
3 | 3 | 6 | score = 2 + 1 = 3 sum = 3 + 3 = 6 |
4 | 4 | 10 | score = 3 + 1 = 4 sum = 6 + 4 = 10 Test condition becomes false. Loop terminates. |
Question 2(iii)
Write the Java statement for the following mathematical expression
Answer
p = a / Math.pow(b,2) + b / Math.pow(a, 2);
Question 2(iv)
If a = 8, find the value of a -= ++a + a++ + 4 - a + 6.
Answer
a = -10
Explanation
The given expression is evaluated as follows:
a -= ++a + a++ + 4 - a + 6
a = a - (++a + a++ + 4 - a + 6) [a = 8]
a = 8 - (9 + a++ + 4 - a + 6) [a = 9]
a = 8 - (9 + 9 + 4 - 10 + 6) [a = 10]
a = 8 - 18
a = -10
Question 2(v)
Rewrite the following code using for loop.
int i = 1;
int d = 5;
do
{
d = d * 2;
System.out.println(d);
i++;
} while(i <= 5);
Answer
int d = 5;
for(int i = 1; i <= 5; i++)
{
d = d * 2;
System.out.println(d);
}
Question 2(vi)
Name the operators listed below.
- ?:
- &&
- <=
- ++
Answer
Ternary operator.
Logical AND operator.
Less than or equal relational operator.
Increment Operator.
Question 2(vii)
Find out the error(s) in the following code and underlining correct error(s).
int m = 5; n = 10;
while(n >= 1)
{
System.out.print("m");
n--;
}
Answer
There are two errors in this code:
- Variable n is not declared.
- Instead of printing variable m, string "m" is printed inside the loop.
int m = 5, n = 10; //correction 1
while(n >= 1)
{
System.out.print(m); //correction 2
n--;
}
Question 2(viii)
Evaluate the following expressions, when int a = 8, b = 14.
(a) b/a++
(b) --a + b-- + a
Answer
(a) b/a++
b / a++
⇒ 14 / 8 [Postfix operator first uses the value then increments it]
⇒ 1 [Integer division is performed as both a and b are int variables]
Hence, the expression evaluates to 1
(b) --a + b-- + a
--a + b-- + a
⇒ 7 + b-- + a [a = 7, Prefix operator first decrements the value then uses it]
⇒ 7 + 14 + 7 [b = 13, Postfix operator first uses the value then decrements it]
⇒ 28
Hence, the expression evaluates to 28
Question 2(ix)
Write the syntax of switch statement.
Answer
The syntax of switch statement is as follows:
switch (expression)
{
case value1:
statements;
break;
case value2:
statements;
break;
...
...
case valueN:
statements;
break;
default:
statements;
}
Question 2(x)
What will be the output of the following program segment?
int a = 100;
while(false)
{
if(a < 50)
break;
a = a - 10;
}
System.out.println(a);
Answer
Output
100
Explanation
Since the condition of while loop is false, the body of while loop will not execute. The print statement following the loop will execute and print 100 on the screen.
Section B
Question 3
Define a class with the following specifications
Class name : employee
Member variables
eno : employee number
ename : name of the employee
age : age of the employee
basic : basic salary (Declare the variables using appropriate data types)
Member methods
void accept( ) : accept the details using scanner class
void calculate( ) : to calculate the net salary as per the given specifications
net = basic + hra + da - pf
hra = 18.5% of basic
da = 17.45% of basic
pf = 8.10% of basic
if the age of the employee is above 50 he/she gets an additional allowance of ₹5000
void print( ) : to print the details as per the following format
eno ename age basic net
void main( ) : to create an object of the class and invoke the methods
import java.util.Scanner;
public class employee
{
private int eno;
private String ename;
private int age;
private double basic;
private double net;
public void accept()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter name : ");
ename = in.nextLine();
System.out.print("Enter employee no : ");
eno = in.nextInt();
System.out.print("Enter age : ");
age = in.nextInt();
System.out.print("Enter basic salary : ");
basic = in.nextDouble();
}
public void calculate()
{
double hra, da, pf;
hra = 18.5/100.0 * basic;
da = 17.45/100.0 * basic;
pf = 8.10/100.0 * basic;
net = basic + hra + da - pf;
if(age > 50)
net += 5000;
}
public void print()
{
System.out.println(eno + "\t" + ename + "\t" + age + "\t Rs."
+ basic + "\t Rs." + net );
}
public static void main(String args[])
{
employee ob = new employee();
ob.accept();
ob.calculate();
ob.print();
}
}
Output
Question 4
Define a class to accept the elements of an array from the user and check for the occurrence of positive number, negative number and zero.
Example
Input Array: 12, -89, -56, 0, 45, 56
Output:
3 Positive Numbers
2 Negative Numbers
1 Zero
import java.util.Scanner;
public class KboatSDANumbers
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int pc = 0;
int nc = 0;
int zc = 0;
System.out.print("Enter size of array : ");
int l = in.nextInt();
int arr[ ] = new int[l];
System.out.println("Enter array elements : ");
for (int i = 0; i < l; i++) {
arr[i] = in.nextInt();
}
for (int i = 0; i < l; i++) {
if (arr[i] < 0)
nc++;
else if(arr[i] > 0)
pc++;
else
zc++;
}
System.out.println(pc + " Positive Numbers");
System.out.println(nc + " Negative Numbers");
System.out.println(zc + " Zero");
}
}
Output
Question 5
Define a class Anagram to accept two words from the user and check whether they are anagram of each other or not.
An anagram of a word is another word that contains the same characters, only the order of characters is different.
For example, NOTE and TONE are anagram of each other.
import java.util.Scanner;
public class Anagram
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the 2 words to check");
System.out.print("Enter first word: ");
String str1 = in.next();
System.out.print("Enter second word: ");
String str2 = in.next();
boolean isAnagram = true;
String s1 = str1.toLowerCase();
String s2 = str2.toLowerCase();
int l1 = s1.length();
int l2 = s2.length();
if (l1 != l2) {
isAnagram = false;
}
else {
int count[] = new int[26];
for (int i = 0; i < l1; i++) {
count[s1.charAt(i) - 97]++;
count[s2.charAt(i) - 97]--;
}
for (int i = 0; i < count.length; i++) {
if (count[i] != 0) {
isAnagram = false;
break;
}
}
}
if (isAnagram)
System.out.println("Anagram");
else
System.out.println("Not Anagram");
}
}
Output
Question 6
Define a class to accept a number and check whether the number is valid number or not. A valid number is a number in which the eventual sum of digits of the number is equal to 1.
e.g., 352 = 3 + 5 + 2 = 10 = 1 + 0 = 1
Then 352 is a valid number.
import java.util.Scanner;
public class KboatValidNum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number : ");
int num = in.nextInt();
int n = num;
while (n > 9) {
int sum = 0;
while (n != 0) {
int d = n % 10;
n /= 10;
sum += d;
}
n = sum;
}
if (n == 1)
System.out.println(num + " is Valid Number");
else
System.out.println(num + " is not Valid Number");
}
}
Output
Question 7
Write the code to print following patterns
(i)
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
(ii)
A A A A A
A A A B B
A A C C C
A D D D D
E E E E E
public class KboatPattern
{
public static void main(String args[]) {
System.out.println("Pattern 1");
for (int i = 1; i <= 5; i++) {
int x = 4;
System.out.print(i + " ");
int t = i;
for (int j = 1; j < i; j++) {
t += x;
System.out.print(t + " ");
x--;
}
System.out.println();
}
System.out.println();
System.out.println("Pattern 2");
char ch = 'A';
for (int i = 1; i <= 5; i++) {
for (int j = 4; j >= i; j--) {
System.out.print("A ");
}
for (int k = 1; k <= i; k++) {
System.out.print(ch + " ");
}
System.out.println();
ch++;
}
}
}
Output
Question 8
Write a program to accept name and total marks of N number of students in two single subscript array name[ ] and totalmarks[ ].
Calculate and print :
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student's total marks with the average.
[deviation = total marks of a student - average]
import java.util.Scanner;
public class KboatSDAMarks
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();
String name[] = new String[n];
int totalmarks[] = new int[n];
int grandTotal = 0;
for (int i = 0; i < n; i++) {
in.nextLine();
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter total marks of student " + (i+1) + ": ");
totalmarks[i] = in.nextInt();
grandTotal += totalmarks[i];
}
double avg = grandTotal / (double)n;
System.out.println("Average = " + avg);
for (int i = 0; i < n; i++) {
System.out.println("Deviation for " + name[i] + " = "
+ (totalmarks[i] - avg));
}
}
}