SECTION A
(Attempt all questions.)
Question 1:
a. Define encapsulation.
Answer: The system of wrapping data and function into a single unit (called class) is known as encapsulation.
b. Explain the process of using ‘new ‘ keyword in a java program.
Answer: The Java new keyword is used to create an instance of the class. In other words, it instantiates a class by allocating memory for a new object and returning a reference to that memory. We can also use the new keyword to create the array object.
c. What are literals?
Answer: Literals are the constants used in a Java program. While writing a program in Java, you may come across some quantities, which remain fixed (ie do not change) throughout the discussion of the program. Such quantities are called constants or literals.
d. Mention the types of access specifiers.
Answer: Following are the four types of access specifiers.
- Private: We can access the private modifier only within the same class and not from outside the class.
- Default: We can access the default modifier only within the same package and not from outside the package. And also, if we do not specify any access modifier it will automatically consider it as default.
- Protected: We can access the protected modifier within the same package and also from outside the package with the help of the child class. If we do not make the child class, we cannot access it from outside the package. So inheritance is a must for accessing it from outside the package.
- Public: We can access the public modifier from anywhere. We can access public modifiers from within the class as well as from outside the class and also within the package and outside the package.
e. What is constructor overloading?
Answer: In Java, we can overload constructors like methods. Constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task.
Question 2:
a. Difference between boxing and unboxing.
Answer: The basic difference between Boxing and Unboxing is that Boxing is the conversion of the value type to an object type whereas, on the other hand, the term Unboxing refers to the conversion of the object type to the value type. Answer: Unboxing is the conversion from the wrapper class to the primitive data type.
b. Rewrite the following condition without using the logical operators:
if(a>b||a>c)
System.out.println(a);
Answer:
if(a>b)
{
if(a>c)
System.out.println(a);
else
System.out.println(a);
}
c. Rewrite the following loop using for loop:
while (true)
System.out.print(“*”);
Answer:
for(;true;)
System.out.print("*");
d. Write the prototype of a function search which takes two arguments a string
and a character and returns an integer value.
Answer: int search(String s, char c)
e. Differentiate between = and == operators.
Answer: The “=” is an assignment operator used to assign the value on the right to the variable on the left, while The ‘==’ operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise, it returns false.
Question 3:
a. State the number of bytes and bits occupied by a character array of 10 elements.
Answer: A character array of 10 elements occupies 20 bytes, which is equivalent to 160 bits (since 1 byte is equal to 8 bits).
b. Differentiate between Binary Search and Linear Search techniques.
Answer:
Binary Search | Linear Search |
1. For a binary search to be performed, the array must be sorted either in ascending or descending order. | 1. Linear search can be performed on both sorted and unsorted arrays. |
2. In a binary search, the array is repeatedly divided into two equal parts and the target element is located by searching within one of these halves. | 2. A linear search involves checking each element of the array in sequence until the target element is found or the end of the array is reached. |
3. Binary Search is faster | 3. Linear Search is slower |
c. What is the output of the following:
String a=”Java is programming language \n developed by \t\’James Gosling\'”; System.out.println(a);
Answer:
Java is programming language
developed by 'James Gosling'
d. Differentiate between break and System.exit(0).
Answer:
break | System.exit(0) |
---|---|
break statement is used to jump out of the loop or switch-case | System.exit(0) terminates the entire program |
break is a keyword | System.exit(0) is a method provided by Java standard library |
e. Write a statement in Java for √((a+b)3÷|a-b|).
Answer: Math.sqrt(Math.pow(a + b, 3) / Math.abs(a – b));
f. What is the value of m after evaluating the following expression:m -= 9%++n + ++n/2; when int m=10,n=6.
Answer: 4
g. Predict output of the following:
(i) Math.pow(25,0.5)+Math.ceil(4.2)
Answer: 10.0
(ii) Math.round ( 14.7 ) + Math.floor ( 7.9)
Answer: 22.0
h. Give the output of the following java statements:
(i) “TRANSPARENT”.toLowerCase();
Answer: transparent
(ii) “TRANSPARENT”.compareTo(“TRANSITION”)
Answer: 7
i. Write a java statement for each to perform the following task:
(i) Find and display the position of the last space in a string str.
Answer: System.out.println(str.lastIndexOf(‘ ‘));
(ii) Extract the second character of the string str.
Answer: System.out.println(str.charAt(1));
j. State the type of errors if any in the following statements:
(i) switch ( n > 2 )
Answer: Syntax Error
(ii) System.out.println(100/0);
Answer: Runtime Error
Section B
Question 4:
Anshul transport company charges for the parcels of its customers as per the following specifications given below:
Class name: Atransport
Member variables:
String name – to store the name of the customer
int w – to store the weight of the parcel in Kg
int charge – to store the charge of the parcel
Member functions:
void accept ( ) – to accept the name of the customer, and weight of the parcel from the user (using Scanner class)
void calculate ( ) – to calculate the charge as per the weight of the parcel as per the following criteria:
Weight in Kg | Charge per Kg |
---|---|
Upto 10 Kgs | Rs.25 per Kg |
Next 20 Kgs | Rs.20 per Kg |
Above 30 Kgs | Rs.10 per Kg |
A surcharge of 5% is charged on the bill.
void print ( ) – to print the name of the customer, weight of the parcel, and total bill inclusive of surcharge in a tabular form in the following format:
Name Weight Bill amount
——- ——- ————-
Define a class with the above-mentioned specifications, create the main method, create an object and invoke the member methods.
import java.util.*;
public class Atransport
{
private String name;
private int w;
private int charge;
public void accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Customer Name: ");
name = in.nextLine();
System.out.print("Enter Parcel Weight: ");
w = in.nextInt();
}
public void calculate() {
if (w <= 10)
charge = w * 25;
else if (w <= 30)
charge = 250 + ((w - 10) * 20);
else
charge = 250 + 400 + ((w - 30) * 10);
charge += charge * 5 / 100;
}
public void print() {
System.out.println("Name\tWeight\tBill amount");
System.out.println("----\t------\t-----------");
System.out.println(name + "\t" + w + "\t" + charge);
}
public static void main(String args[]) {
Main obj = new Main();
obj.accept();
obj.calculate();
obj.print();
}
}
Output:
Enter Customer Name: Manoj
Enter Parcel Weight: 25
Name Weight Bill amount
---- ------ -----------
Manoj 25 577
Question 5: Write a program to input name and percentage of 35 students of class X in two separate one dimensional arrays. Arrange students’ details according to their percentage in descending order using the selection sort method. Display the name and percentage of first ten toppers of the class.
import java.util.Scanner;
public class Main
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
//Initialize the 2 SDA
String names[] = new String[35];
double percentage[] = new double[35];
double t;
String tnane;
int i,j,nax;
/*
Accept student details from user and store in corresponding SDA*/
for (i = 0; i < 35; i++) {
System.out.print("Enter name of student "
+ (i + 1) + ": ");
names[i] = in.nextLine();
System.out.print("Enter percentage of student "
+ (i + 1) + ": ");
percentage[i] = in.nextDouble();
in.nextLine();
}
//Selection Sort in Descending Order
for (i = 0; i < 35 - 1; i++) {
max = i;
for (j = i + 1; j < 35; j++) {
if (percentage[j] > percentage[max])
max = j;
}
t = percentage[i];
percentage[i] = percentage[max];
percentage[max] = t;
name = names[i];
names[i] = names[max];
names[max] = name;
}
//Display first ten toppers
System.out.println("Name\tPercentage");
for (i = 0; i < 10; i++) {
System.out.println(names[i] + '\t' + percentage[i]);
}
}
}
Question 6: Design a class to overload a function Sum( ) as follows:
(i) int Sum(int A, int B) — with two integer arguments (A and B) calculate and return a sum of all the even numbers in the range of A and B.
Sample input: A=4 and B=16
Sample output: sum = 4 + 6 + 8 + 10 + 12 + 14 + 16
(ii) double Sum( double N ) — with one double arguments(N) calculate and return the product of the following series:
sum = 1.0 x 1.2 x 1.4 x . . . . . . . . x N
(iii) int Sum(int N) – with one integer argument (N) calculate and return a sum of only odd digits of the number N.
Sample input: N=43961
Sample output : sum = 3 + 9 + 1 = 13
Write the main method to create an object and invoke the above methods.
public class Main
{
public int Sum(int A, int B) {
int s= 0;
for (int i = A; i <= B; i++) {
if (i % 2 == 0)
s=0;
}
return s;
}
public double Sum(double N)
{
double p,i;
p = 1.0;
for (i = 1.0; i <= N; i += 0.2)
p=p*i;
return p;
}
public int Sum(int N)
{
int s,d;
s=0;
while (N != 0)
{
d = N % 10;
if (d % 2 != 0)
s=s*d;
N /= 10;
}
return s;
}
public static void main(String args[]) {
Main obj = new Main();
System.out.println("Even sum from 4 to 16 = " + obj.Sum(4, 16));
System.out.println("Series Product = " + obj.Sum(2.0));
System.out.println("Odd Digits Sum = " + obj.Sum(43961));
}
}
Output:
Even sum from 4 to 16 = 0
Series Product = 9.676799999999997
Odd Digits Sum = 0
Question 7: Using the switch statement, write a menu driven program to perform following operations:
(i) To Print the value of Z where Z = (x3 + 0.5x) / Y where x ranges from – 10 to 10 with an increment of 2 and Y remains constant at 5.5.
(ii) To print the Floyds triangle with N rows
Example: If N = 5, Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14
Answer:
import java.util.Scanner;
public class Main
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Value of Z");
System.out.println("Type 2 for Floyd\'s triangle");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch)
{
case 1:
double z, y = 5.5;
int x;
for (x = -10; x <= 10; x = x+2)
{
z = (Math.pow(x, 3) + 0.5 * x) / y;
System.out.println("Value of Z when x is "
+ x + " = " + z);
}
break;
case 2:
int i,j,k,N;
System.out.print("Enter number of rows: ");
N = in.nextInt();
k = 1;
for (i = 1; i <= N; i++)
{
for (j = 1; j <= i; j++)
{
System.out.print(k+ " ");
k++;
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice !!!");
break;
}
}
}
Output:
Type 1 for Value of Z
Type 2 for Floyd's triangle
Enter your choice: 1
Value of Z when x is -10 = -182.72727272727272
Value of Z when x is -8 = -93.81818181818181
Value of Z when x is -6 = -39.81818181818182
Value of Z when x is -4 = -12.0
Value of Z when x is -2 = -1.6363636363636365
Value of Z when x is 0 = 0.0
Value of Z when x is 2 = 1.6363636363636365
Value of Z when x is 4 = 12.0
Value of Z when x is 6 = 39.81818181818182
Value of Z when x is 8 = 93.81818181818181
Value of Z when x is 10 = 182.72727272727272
Type 1 for Value of Z
Type 2 for Floyd's triangle
Enter your choice: 2
Enter number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Question 8: Write a program to input and store integer elements in a double dimensional array of size 4×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
Answer
import java.util.Scanner;
public class Main
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int i,j;
int arr[][] = new int[4][4];
long s = 0;
System.out.println("Enter the elements of 4x4 array:");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
arr[i][j] = in.nextInt();
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
System.out.print(arr[i][j]);
}
System.out.println();
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
s = s + arr[i][j];
}
}
System.out.println("Sum of all the elements: " + s);
}
}
Output:
Enter the elements of 4x4 array:
7
3
4
5
5
4
6
1
6
9
4
2
3
2
7
5
7345
5461
6942
3275
Sum of all the elements: 73
Question 9: Write a program to input a string and convert it into uppercase and print the pair of vowels and number of pair of vowels occurring in the string.
Example:
Input:
“BEAUTIFUL BEAUTIES “
Output :
Pair of vowels: EA, AU, EA, AU, IE
No. of pair of vowels: 5
Answer:
import java.util.Scanner;
public class Main
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String str;
char ch,ch1;
int i,l,c;
System.out.println("Enter the sentence:");
str = in.nextLine();
str = str.toUpperCase();
l=str.length();
c = 0;
System.out.print("Pair of vowels: ");
for (i = 0; i < l - 1; i++)
{
ch=str.charAt(i);
ch1=str.charAt(i+1);
if ((ch == 'A'||ch == 'E'||ch == 'I'||ch == 'O'||ch == 'U')&&((ch1 == 'A' ||ch1 == 'E' ||ch1 == 'I' ||ch1 == 'O' ||ch1 == 'U')))
{
c++;
System.out.print(ch);
System.out.print(ch1 + " ");
}
}
System.out.print("\nNo. of pair of vowels: " + c);
}
}
Output:
Enter the sentence:
BEAUTIFUL BEAUTIES
Pair of vowels: EA AU EA AU IE
No. of pair of vowels: 5