Section A
Question 1(i)
Which of the following is/are principle(s) of Object Oriented Programming?
- Encapsulation
- Abstraction
- Inheritance
- All of these
Answer
All of these
Reason — Encapsulation, abstraction and inheritance are principles of Object Oriented Programming.
Question 1(ii)
If a = 8 and b = 4, the value of a % = b
is
- 2
- 0
- 4
- 8
Answer
0
Reason — The expression a % = b
is evaluated as follows:
a % = b
⟹ a = a % b
⟹ a = 8 % 4
⟹ a = 0
Question 1(iii)
Predict the output of the following code snippet:
int a = 1;
int b = 2;
if (a == b)
System.out.println("Both values are equal");
else
System.out.println("Values are not equal");
- Both values are equal
- Incorrect use of the == operator
- Values are not equal
- No output
Answer
Values are not equal
Reason — Since the values of a and b are not equal, the condition (a == b)
is false. Thus, the else block is executed and "Values are not equal" is printed.
Question 1(iv)
Which of the following is mandatory in the switch statement?
- break
- continue
- case
- default
Answer
case
Reason — The keyword 'case' is mandatory in the switch statement.
Question 1(v)
Which of the following is not a valid method of the Scanner class?
- next( )
- nextInt( )
- nextLong( )
- nextNumber( )
Answer
nextNumber( )
Reason — nextNumber( ) is not a valid method of the Scanner class.
Question 1(vi)
What will be the output of Math.floor(-20.10);
?
- -20.0
- -21.0
- 20
- 21
Answer
-21.0
Reason — Math.floor() method returns the largest double value that is less than or equal to the argument and is equal to a mathematical integer. Thus, -21.0 is returned by Math.floor(-20.10);
method.
Question 1(vii)
Which of the following is an illegal identifier?
- age
- 123abc
- _value
- salary
Answer
123abc
Reason — 123abc is not a valid identifier because it starts with a number, which is not allowed.
Question 1(viii)
Which of the following is/are jump statement used in Java?
- break
- continue
- return
- All of these
Answer
All of these
Reason — break, continue and return are jump statements used in Java.
Question 1(ix)
Class initialisation is the ............... initialisation of class fields of values.
- explicit
- implicit
- Both a and b
- None of the above
Answer
explicit
Reason — Explicit class initialisation is the initialisation of class fields of values.
Question 1(x)
Operations like square root, sine and cosine are
- impure functions
- pure functions
- static functions
- class functions
Answer
pure functions
Reason — Operations like square root, sine and cosine are pure functions as they do not modify their arguments.
Question 1(xi)
Given array int x[ ] = {11, 22, 33, 44}; the value of x[1 + 2] is :
- 11
- 22
- 33
- 44
Answer
44
Reason — Array index starts from 0 to Size - 1. The value of x[1 + 2]
equals x[3]
. The value at index 3 is 44.
Question 1(xii)
A linear 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 both sorted and unsorted arrays.
Reason — A linear search can be used with both sorted and unsorted arrays.
Question 1(xiii)
Method that converts a character to uppercase is
- toUpper()
- ToUpperCase()
- TOUPPERCASE()
- toUpperCase(char)
Answer
toUpperCase(char)
Reason — Method that converts a character to uppercase is toUpperCase(char).
Question 1(xiv)
Give the output of the following code
System.out.println("Good".concat("Day"));
- GoodDay
- Good Day
- Goodday
- goodDay
Answer
GoodDay
Reason — concat() method will add "Day" at the end of "Good" and "GoodDay" will be printed on the screen.
Question 1(xv)
The array char arr[8] occupies :
- 32
- 8
- 16
- 24
Answer
16
Reason — char datatype requires 2 bytes of memory. 8 char type elements will require 2 x 8 = 16 bytes of memory.
Question 1(xvi)
This access specifier achieves the lowest level of accessibility.
- Public
- Protected
- Private
- Default
Answer
Private
Reason — Private access specifier achieves the lowest level of accessibility as a data member or member method declared as private is only accessible inside the class in which it is declared.
Question 1(xvii)
Assertion (A) Line comment is used for a single line of comment.
Reason (R) A line comment starts with forward slash and asterisk(/*).
- 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
Assertion (A) is true and Reason (R) is false.
Reason — Assertion (A) is true as a line comment is used for a single line of comment. Reason (R) is false as a line comment starts with two forward slashes (//).
Question 1(xviii)
Read the following text and choose the correct answer:
Byte streams are used to perform input and output of 8-bytes. They are used to read bytes from the input stream and write bytes to the output stream. Mostly, they are used to read or write raw binary data.
Which of these class is used to read and write bytes in a file?
- FileReader
- FileWriter
- FileInputStream
- InputStreamReader
Answer
FileInputStream
Reason — FileInputStream class is used to read and write bytes in a file.
Question 1(xix)
Assertion (A) The private access specifier achieves the lowest level of accessibility.
Reason (R) The private methods and fields can be accessed only within the same class to which the methods and fields belong.
- 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 — Both Assertion (A) is true as private access specifier achieves the lowest level of accessibility and Reason (R) explains Assertion (A) correctly as a data member or member method declared as private is only accessible inside the class in which it is declared.
Question 1(xx)
State the data type after the following is executed :
char ch = '9';
res = Character.isDigit(ch);
- int
- char
- string
- boolean
Answer
boolean
Reason — isDigit() method returns true if the specified character is a digit; returns false otherwise. Thus, the return type is boolean.
Question 2(i)
State the number of bytes and bits occupied by a character array of 20 elements.
Answer
A char data type occupies 2 bytes in memory.
Total memory occupied by an array of 20 elements = 2 x 20 = 40 bytes.
Since 1 byte = 8 bits, memory occupied by an array of 20 elements = 40 x 8 = 320 bits.
Question 2(ii)
If
String x = "Computer";
String y = "Science";
What will the following return?
(a) System.out.print(x.substring(2,6));
(b) System.out.print(x.indexOf(x.charAt(5)));
Answer
(a)
Output
mput
Explanation
The substring() method returns a substring beginning from the startindex and extending to the character at index endIndex - 1. Here, startindex is 2 and end index is 5 (6 - 1). Since the string starts from index 0, the extracted substring is "mput". Thus, "mput" is printed on the screen.
(b)
Output
5
Explanation
charAt() method returns a character from the string at the index specified as its argument. indexOf() method returns the index of the first occurrence of the specified character within the string or -1 if the character is not present. Thus, the given expression is evaluated as follows:
x.indexOf (x.charAt (5) )
⟹ x.indexOf ( 't' )
⟹ 5
Question 2(iii)
Write the Java statement for the following mathematical expression :
x =
Answer
x = Math.pow((a + b), 2) / Math.sqrt(a + b);
Question 2(iv)
Find the value of ++a * (a++ + 5) + 3 * --a
, if a = 12.
Answer
The given expression is evaluated as follows:
++a * (a++ + 5) + 3 * --a (a = 12)
⟹ 13 * (a++ + 5) + 3 * --a (a = 13)
⟹ 13 * (13 + 5) + 3 * --a (a = 14)
⟹ 13 * (13 + 5) + 3 * 13 (a = 13)
⟹ 13 * (18) + 3 * 13
⟹ 234 + 39
⟹ 273
Question 2(v)
State the type of errors, if any in the following statements.
(a)
switch (x < 2)
(b)
int a = 100, b = 0;
System.out.println (a / b);
Answer
(a) There is a syntax error in the switch statement. In a switch statement, the expression inside the parentheses should evaluate to an integer, a character, or an enumerated type. However, x < 2 is a boolean expression, and it cannot be used directly as the expression in a switch statement.
(b) The statement System.out.println (a / b);
will result in a runtime error as the user is trying to divide a number by 0 which is not possible.
Question 2(vi)
What will be the output of the following code?
int num = 10;
if (num < 20)
System.out.print(num++);
else
System.out.print(--num);
Answer
Output
10
Explanation
Since the condition (num < 20) is true, the if block is executed. Postfix operator first uses the value and then increments the value, so the value of num is first printed (10) and then incremented.
Question 2(vii)
Find the output of the given code.
int a, b = 100;
for (a = 10; a <= 12; a++)
{
b += a;
}
System.out.print("a:" + a + " " + "b:" + b);
Answer
Output
a:13 b:133
Explanation
The values of a and b are evaluated as follows in the for loop:
Iteration | a | b | Remarks |
---|---|---|---|
100 | Initial value | ||
1 | 10 | 110 | b = b + a = 100 + 10 = 110 |
2 | 11 | 121 | b = b + a = 110 + 11 = 121 |
3 | 12 | 110 | b = b + a = 121 + 12 = 133, Loop terminates |
The print statements print the final values of a and b.
Question 2(viii)
The following code has some error(s). Identify the errors and write the corrected code.
Int x = 4, y = 8;
{
y = y + (x++)
} while (x <= 10)
System.out.println(y);
Answer
The corrected code is as follows:
int x = 4, y = 8;
do
{
y = y + (x++);
} while (x <= 10);
System.out.println(y);
Statement | Error |
---|---|
Int x = 4, y = 8; | int is a keyword and should be written in lowercase |
The user needs to use do while loop | The do-while loop structure is used incorrectly, as the do keyword is missing. |
y = y + (x++) | Semicolon should be written at the end of the statement |
while (x <= 10) | A semicolon should be written after the while(x <= 10) |
Question 2(ix)
State the method that determines, if the specified character is an uppercase character.
Answer
isUpperCase(char) method determines, if the specified character is an uppercase character. It returns true if the character is upper case, else it returns false.
Question 2(x)
Write the return data type of the following functions.
(a) startsWith( )
(b) log( )
Answer
(a) boolean
(b) double
Section B
Question 3
Define a class to declare an array of size 20 of double datatype, accept the elements into the array and perform the following:
Calculate and print the sum of all the elements.
Calculate and print the highest value of the array.
import java.util.Scanner;
public class KboatSDASumMax
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double arr[] = new double[20];
System.out.println("Enter 20 numbers:");
for (int i = 0; i < 20; i++) {
arr[i] = in.nextDouble();
}
double max = arr[0], sum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max)
max = arr[i];
sum += arr[i];
}
System.out.println("Sum = " + sum);
System.out.println("Highest Value = " + max);
}
}
Output
Question 4
Write a program to print following patterns.
(i)
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
(ii)
A
B A
C B A
D C B A
E D C B A
public class KboatPattern
{
public static void main(String args[]) {
System.out.println("Pattern 1: ");
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
System.out.println();
System.out.println("Pattern 2: ");
char ch = 'A';
for (int i = 0; i < 5; i++) {
for (int j = i; j >= 0; j--) {
System.out.print((char)(65 + j) + " ");
}
System.out.println();
}
}
}
Output
Question 5
Write a program to input and store integer elements in a double dimensional array of size 4 x 4 and find the sum of all the elements.
7 3 4 5
5 4 6 1
6 9 4 2
3 2 7 5
Sum of all the elements: 73
import java.util.Scanner;
public class KboatDDASum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[][] = new int[4][4];
long sum = 0;
System.out.println("Enter the elements of 4 x 4 DDA: ");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
arr[i][j] = in.nextInt();
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
sum += arr[i][j];
}
}
System.out.println("Sum of all the elements = " + sum);
}
}
Output
Question 6
Write a program to input a number and check whether the number is an automorphic number or not.
An automorphic number is a number whose square "ends" in the same digits as the number itself.
e.g. 52 = 25, 62 = 36, 762 = 5776
import java.util.Scanner;
public class KboatAutomorphic
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
int numCopy = num;
int sq = num * num;
int d = 0;
/*
* Count the number of
* digits in num
*/
while(num > 0) {
d++;
num /= 10;
}
/*
* Extract the last d digits
* from square of num
*/
int ld = (int)(sq % Math.pow(10, d));
if (ld == numCopy)
System.out.println(numCopy + " is automorphic");
else
System.out.println(numCopy + " is not automorphic");
}
}
Output
Question 7
Design a class RailwayTicket with following description:
Class name : RailwayTicket
Data Members | Purpose |
---|---|
String name | To store the name of the customer |
String coach | To store the type of coach customer wants to travel |
long mob no | To store customer's mobile number |
int amt | To store basic amount of ticket |
int totalamt | To store the amount to be paid after updating the original amount |
Member Methods | Purpose |
---|---|
void accept() | To take input for name, coach, mobile number and amount |
void update() | To update the amount as per the coach selected (extra amount to be added in the amount as per the table below) |
void display() | To display all details of a customer such as name, coach, total amount and mobile number |
Type of Coaches | Amount |
---|---|
First_AC | ₹700 |
Second_AC | ₹500 |
Third_AC | ₹250 |
Sleeper | None |
Write a main method to create an object of the class and call the above member methods.
import java.util.Scanner;
public class RailwayTicket
{
private String name;
private String coach;
private long mobno;
private int amt;
private int totalamt;
private void accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
name = in.nextLine();
System.out.print("Enter coach: ");
coach = in.nextLine();
System.out.print("Enter mobile no: ");
mobno = in.nextLong();
System.out.print("Enter amount: ");
amt = in.nextInt();
}
private void update() {
if(coach.equalsIgnoreCase("First_AC"))
totalamt = amt + 700;
else if(coach.equalsIgnoreCase("Second_AC"))
totalamt = amt + 500;
else if(coach.equalsIgnoreCase("Third_AC"))
totalamt = amt + 250;
else if(coach.equalsIgnoreCase("Sleeper"))
totalamt = amt;
}
private void display() {
System.out.println("Name: " + name);
System.out.println("Coach: " + coach);
System.out.println("Total Amount: " + totalamt);
System.out.println("Mobile number: " + mobno);
}
public static void main(String args[]) {
RailwayTicket obj = new RailwayTicket();
obj.accept();
obj.update();
obj.display();
}
}
Output
Question 8
Write a program to accept a string. Convert the string into upper case letters. Count and output the number of double letter sequences that exist in the string.
Sample Input: "SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE"
Sample Output: 4
import java.util.Scanner;
public class KboatLetterSeq
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter string: ");
String s = in.nextLine();
String str = s.toUpperCase();
int count = 0;
int len = str.length();
for (int i = 0; i < len - 1; i++) {
if (str.charAt(i) == str.charAt(i + 1))
count++;
}
System.out.println("Double Letter Sequence Count = " + count);
}
}