Computers
Computers
Question 1: [20]
Choose the correct answer and write the correct option.
i) An instance of a class is termed as:
(a) Attribute (c) Value
(b) Message (d) Object
Ans: (d) Object
ii) A constructor:
(a) Is always public (c) Never returns a value
(b) Never has a same name as class (d) Both (a) and (b)
Ans: (c) Never returns a value
iii) In which way must the array be sorted for binary search to be applicable on it?
(a) sorting not required (c) ascending
(b) descending (d) either (c) or (b)
Ans: (d) either (c) or (b)
iv) How many times the inner loop will be executed?
for(int k=1; k<=2; k++) {
for(int m=1; m<=4; m++) {
System.out.println(m*2);
} }
(a) 8 times (c) 2 times
(b) 4 times (d) 16 times
Ans: (a) 8 times
v) What will be the final value of x, if initial value of x = 5?
x = x++ * 2 + 3 * –x;
(a) 7 (c) -7
(b) -8 (d) 9
Ans: (b) -8
vi) Which one is a single line comment from below?
(a) /*comment*/ (c) //comment
(b) * comment (d) /* comment
Ans: (c) //comment
vii) Which method checks if a character is an alphabet or not?
(a) isLetter(char) (c) isAlpha(char)
(b) isUppercase(char) (d) isLowercase(char)
Ans: (a) isLetter(char)
viii) The output of Double.parseDouble("71.25") + 0.75 is:
(a) 72 (c) 71.75
(b) 71.0 (d) 72.0
Ans: (d) 72.0
ix) The element in index 4 of the array {3, 5, 7, 12, 16, 18, 20, 35, 42, 89} is:
(a) 12 (c) 18
(b) 16 (d) 7
Ans: (b) 16
x) All the basic data types of Java are also known as:
(a) Basic types (c) Object types
(b) None of these (d) Primitive types
Ans: (d) Primitive types
pg. 1
xi) What will be the output of the following program snippet:
String A ="26", B ="100";
String D =A+B+"200";
int x= Integer.parseInt(A);
int y = Integer.parseInt(B);
int d = x+y;
System.out.println("Result 1 = " + D);
System.out.println("Result 2 = " + d);
(a) Result 1 = 26100200 (c) Result 1 = 26100200
Result 2 = 126 Result 2 = 100
(b) Result 1 = 26100 (d) Result 1 = 26100200
Result 2 = 126 Result 2 = 26
Ans: (a) Result 1 = 26100200
Result 2 = 126
xii) How many searches it will require to successfully search for element 2 using linear search
from the below array?
15 12 2 3 4 5
(a) 2 (c) 4
(b) 3 (d) 5
Ans: (b) 3
xiii) What will be the output of the following java statement:
System.out.println(Math.pow(625,0.5) + Math.sqrt(144));
(a) 17.0 (c) 13.0
(b) 13 (d) 37.0
Ans: (d) 37.0
xiv) What are the values of x and y when the below java code is executed where a = 63, b = 36
boolean x = (a < b) ? true : false; int y = (a > b) ? a : b;
(a) x = true and y = 63 (c) x = true and y = 36
(b) x = false and y = 63 (d) x = false and y = 36
Ans: (b) x = false and y = 63
xv) The output of the method "DETERMINATION".substring(2,6) is:
(a) TERMI (c) Term
(b) term (d) TERM
Ans: (d) TERM
xvi) Which of the following declaration and initialization is the below statement equivalent to?
String str = new String();
(a) String str = "1"; (c) String str = null;
(b) String str = "0"; (d) String str = "\0";
Ans: (c) String str = null;
xvii) Variable that is declared with in the body of a method is termed as:
(a) Instance variable (c) Argument variable
(b) class variable (d) Local variable
Ans: (d) Local variable
xviii) Name the type of error that occurs for the following statement:
System.out.println(math.sqrt(24-25));
(a) run time error (c) syntax error
(b) logical error (d) no error
Ans: (c) syntax error
xix) Multiple branching statement of java is:
(a) for (c) while
(b) switch (d) do-while
Ans: (b) switch
xx) What is the type of casting shown by the following example?
double x = 15.2;
int y = (int) x;
(a) Explicit casting (c) autoboxing
(b) unboxing (d) Implicit casting
Ans: (a) Explicit casting
pg. 2
Question 2: [20]
i) Write a Java statement for the following:
pg. 3
ix) Differentiate between length and length() function.
Ans:
length length()
The length identifier is used to find the The length() function is used to find
length of an array, that means the the length of a String object, i.e., the
number of elements in an array. number of characters in a string
x) What is meant by precedence of operators?
Ans: Precedence of operators refers to the order in which the operators are applied
to the operands in an expression.
Question 3. [15]
Define a class named BookFair with the following specifications:
Class: BookFair
Instance variables/data members:
String Bname - To store the name of the book.
double price - To store the price of the book.
Member methods:
(i) BookFair() - Constructor to initialize data members.
(ii) void input() - To input and store the name, price of the book.
(iii) void calculate() - To calculate the price after discount.
(iv) void display() - To display the name of the book and the price after discount.
Discount is calculated based on the following criteria as per the table below.
Price Discount
Less than or equal to ₹ 1000 2% of price
More than ₹ 1000 and less than or equal to ₹ 3000 10% of price
More than ₹ 3000 15% of price
Write a main method to create an object of the class and call the above member methods.
Ans:
import java.util.*;
public class BookFair
{
String Bname;
double price;
BookFair()
{
Bname = "";
price = 0.0d;
}
public void input()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the Book: ");
Bname = in.nextLine();
System.out.println("Enter the price of the Book: ");
price = in.nextDouble();
}
pg. 4
public void calculate()
{
if (price<= 1000)
price = price – 0.02 * price;
else if (price >1000 && price <= 3000)
price = price – 0.10 * price;
else
price = price – 0.15 * price;
}
public void display()
{
System.out.println("Name of the book is: "+ Bname);
System.out.println("Price of the book after discount is: "+ price);
}
public static void main()
{
BookFair obj = new BookFair();
obj.input();
obj.calculate();
obj.display();
}
}
Name Type Description
Bname String To store the name of the book.
price double To store the price of the book.
Question 4. [15]
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 the digits.)
Example: Consider the number 1124
Sum of the digits = 1 + 1 + 2 + 4 = 8 and Product of the digits = 1 * 1 * 2 * 4 = 8
Ans:
import java.util.*;
public class SpyNumber
{
public static void main()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = scanner.nextInt();
int sumOfDigits = 0;
int productOfDigits = 1;
while (n > 0) {
int remainder = n % 10;
sumOfDigits = sumOfDigits + remainder;
productOfDigits = productOfDigits * remainder;
n = n / 10;
}
if (sumOfDigits == productOfDigits)
System.out.println("Spy number");
else
System.out.println("Not a spy number");
}
}
pg. 5
Name Type Description
n int To store the number
sumOfDigits int To store the sum of digits
productOfDigits int To store the product of digits
remainder int To store the remainder
Question 5. [15]
Design a class to overload a function SumSeries() as follows:
(i) void SumSeries(int n, double x) – with one integer argument and one double argument
to find and display the sum of the series given below:
s = (x/1) – (x/2) + (x/3) – (x/4) + (x/5) … to n terms
(ii) void SumSeries() – To find and display the sum of the following series:
s = 1 + (1 * 2) + (1 * 2 * 3) + … + (1 * 2 * 3 * 4 * … 20)
You need not write the main() function
Ans.
public class Sum_Series {
public void SumSeries(int n, double x)
{
double sum = 0;
for (int i = 1; i <= n; i++)
{
if (i % 2 == 1)
{
sum = sum + (x / i);
}
else
{
sum = sum - (x / i);
}
}
System.out.println("Sum = " + sum);
}
public void SumSeries()
{
int sum = 0;
for (int i = 1; i <= 20; i++)
{
int product = 1;
for (int j = 1; j <= i; j++)
{
product = product * j;
}
sum = sum + product;
}
System.out.println("Sum = " + sum);
}
}
pg. 6
Question 6. [15]
Write a program to accept a string and check whether a string is a palindrome or not. Also print
the length of the string.
Note: A palindrome is a string that reads the same from left to right and vice versa.
Ans:
import java.util.*;
public class Palindrom_string
{
public static void main()
{
Question 7: [15]
Define a class to write parameterized methods with the following specifications:
(i) void factors(int n) - To find and display all the factors of a number input by the user
(including 1 and excluding number itself).
Sample Input: 15
Sample Output: 1, 3 and 5
(ii) void factorial(int n) - To find and display the factorial of a number input by the user (a
factorial of a non-negative integer n, denoted by n! is the product of all integers less than or
equal to n).
Sample Input: 5
Sample Output: 5! = 1*2*3*4*5 = 120
Write a main method to create an object of the class and call the above member methods.
pg. 7
Ans:
import java.util.*;
public class FactorsDemo
{
int n, f=1;
public void factors(int n)
{
for(int i=1; i<n; i++)
{
if(n%i ==0)
System.out.print(i + ",");
}
}
public void factorial(int n)
{
for(int i=1; i<= n; i++)
{
f = f *i;
if(i< n)
{
System.out.print(i + "*");
}
else
{
System.out.print(i + "="+f);
}
}
}
public static void main()
{
FactorsDemo obj = new FactorsDemo();
obj.factors(n);
obj.factorial(n);
}
}
Name Type Description
n int To store the number.
i int for loop variable.
f int To store the factorial of the number.
Question 8: [15]
Write a program to input integer elements into an array of size 10 and perform the following
operations:
i) Display largest number from the array
ii) Display smallest number from the array
iii) Display sum of all the elements of the array
Ans:
import java.util.*;
public class ArrayOperations
{
public static void main()
{
Scanner scanner = new Scanner(System.in);
int numbers[] = new int[10];
System.out.print("Enter 10 numbers: ");
pg. 8
for (int i = 0; i < 10; i++)
{
numbers[i] = scanner.nextInt();
}
int smallest = numbers[0];
int largest = numbers[0];
int sum = 0;
for (int i = 0; i < 10; i++)
{
if (numbers[i] < smallest)
{
smallest = numbers[i];
}
if (numbers[i] > largest)
{
largest = numbers[i];
}
sum = sum + numbers[i];
}
System.out.println("Smallest = " + smallest);
System.out.println("Largest = " + largest);
System.out.println("Sum = " + sum);
}
}
******************************************************************
pg. 9