SECTION A
(Attempt all questions.)
Question 1:
a. Name any two basic principles of Object-oriented programming.
Answer: The system of wrapping data and function into a single unit (called class) is known as encapsulation.
Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces.
b. Write a difference between unary and binary operator.
Answer:
Unary Operators | Binary Operators |
(i) The operators which act upon a single operand are called unary operators. | (i) The operators which require two operands for their action are called binary operators. |
(ii) They are pre-increment and post increment (+ +). | (ii) They are mathematical operators and relational operators. |
c. Name the keyword which:
I. indicates that a method has no return type.
ii. makes the variable as a class variable.
Answer: i. void ii. static
d. Write the memory capacity (storage size) of short and float data types in bytes.
Answer: short has the capacity to store 2 bytes while float can store 4 bytes.
e. Identify and name the following tokens:
i. public
ii. ‘a’
iii. ==
iv. {}
Answer: i. keyword
ii. literal
iii. operator
iv. separator
Question 2:
a. Differentiate between if else if and switch-case statements.
Answer:
S.No. | If-Else Statement | Switch Case Statement |
---|---|---|
1 | This statement is executed based on the condition inside the if-else statement. | Switch statements execute as per the user decision. |
2 | Here we need to use multiple statements for numerous decisions. | Here, we need to use a single statement for numerous decisions. |
3 | This statement is used to choose between two options. | This statement is used to choose among multiple options. |
4 | If-else enforces linear search. | Switch statement enforces binary search. |
5 | The if-else statement estimates integers, characters, pointers, floating points, and boolean types. | The switch statement estimates integers and character expressions. |
6 | One statement will be executed. It can be if or else. | Here, each case will be executed one after the other. |
7 | In if-else, the values are based on conditions. | In the switch case, the values are based on user preference. |
8 | In case, the situation gets false in the if statement, it will automatically execute the else statement. | In any situation, if the switch statement does not find any match, the default condition is executed if created. |
Give the output of the following code:
String P = “20”, Q = “19”,
int a = Integer .parselnt(P);
int b = Integer. valueOf(Q);
System.out.println(a+””+b);
Answer: 2019
c. What are the various types of errors in Java?
Answer: Syntax error, Runtime error, Logical error.
d.
State the data type and value of res after the following is executed :
char ch = ‘9’;
res = Character. isDigit(ch);
Answer: The function isDigit() returns a boolean value, and after the execution of the above expression res will store true.
e. What is the difference between the linear search and the binary search technique?
Answer:
Linear Search | Binary Search |
Linear search works both for sorted and unsorted data. | Binary search works on sorted data (either in ascending order or in descending order). |
Linear search begins at the start of an array i.e. at the 0th position. | This technique divides the array in two halves, and the desired data item is searched in the halves. |
Question 3:
a. Write a Java expression for the following: |x2+2xy|
Answer: Math.abs((x * x) + (2 * x * y));
b. Write the return data type of the following function:
i. startsWith( )
ii. random()
Answer: i. boolean
ii. double
c. If the value of,
what will be the value of tax after the following statement is executed?
Answer: 200
d. Give the output of following code and mention how many times the loop will execute?
int i;
for(i=5; i> =l;i--)
{
if(i%2 ==1)
continue;
System.out.print(i+ "");
}
Answer: 4 2
The loop will execute 5 times.
e.
State a difference between call by value and call by reference.
Answer:
Call By Value | Call By Reference |
---|---|
While calling a function, we pass the values of variables to it. Such functions are known as “Call By Values”. | While calling a function, instead of passing the values of variables, we pass the address of variables(location of variables) to the function known as “Call By References. |
In this method, the value of each variable in the calling function is copied into corresponding dummy variables of the called function. | In this method, the address of actual variables in the calling function is copied into the dummy variables of the called function. |
With this method, the changes made to the dummy variables in the called function have no effect on the values of actual variables in the calling function. | With this method, using addresses we would have access to the actual variables and hence we would be able to manipulate them. |
f. Give the output of the following.
Answer: 4.0
g. Write the output for the following :
String s1 = “phoenix”; String s2 =”island”;
System.out.prinln (s1.substring(0).concat (s2.substring(2)));
System.out.println(s2.toUpperCase( ));
Answer: phoenix land
ISLAND
h. Evaluate the following expression if the value of.
v = x + –- z + y + + + y.
Answer: 9
i. String x[ ] = {“Artificial intelligence”, “IOT”, “Machine learning”, “Big data”};
Give the output of the following statements:
(i) System.out.prindn(x[3]);
(ii) System.out.prindn(x.length);
Answer: i. Big data
ii. 4
j. What is meant by a package? Give an example.
Answer: A package in java is a mechanism for organizing Java classes into namespaces similar to the modules of the module. Java packages allow classes in the same package to access each other’s, package-access members.
Two Java application programming interface packages are java.lang and java.io.
Section B
Question 4:
Design a class name ShowRoom with the following description :
Instance variables/ Data members :
String name – To store the name of the customer
long mobno – To store the mobile number of the customer
double cost – To store the cost of the items purchased
double dis – To store the discount amount
double amount – To store the amount to be paid after discount
Member methods: –
ShowRoom() – default constructor to initialize data members
void input() – To input customer name, mobile number, cost
void calculate() – To calculate a discount on the cost of purchased items, based on following criteria
Cost | Discount (in percentage) |
Less than or equal to ₹ 10000 | 5% |
More than ₹ 10000 and less than or equal to ₹ 20000 | 10% |
More than ₹ 20000 and less than or equal to ₹ 35000 | 15% |
More than ₹ 35000 | 20% |
void display() – To display customer name, mobile number, and amount to be paid after discount
Write a main method to create an object of the class and call the above member methods.
import java.util.*;
class ShowRoom
{
String name;
long mobno;
double cost;
double dis;
double amount;
ShowRoom( )
{
name = "";
mobno =0;
cost = 0;
dis = 0;
amount = 0;
}
void input( )
{
Scanner sc = new Scanner(System.in);
System.out.println("EnterName:");
name = sc.nextLine( );
System.out.println("Enter Mobile number:");
mobno = sc.nextLong( );
System.out.println("Enter cost:");
cost = sc.nextDouble( );
}
void calculate( )
{
if (cost <= 10000)
{
dis=cost*5/100.0;
amount = cost - dis;
}
elseif (cost > 10000 && cost <= 20000)
{
dis = cost* 10/100.0;
amount=cost - dis;
}
elseif (cost > 20000 && cost <= 35000)
{
dis = cost* 15/100.0;
amount = cost - dis;
}
elseif(cost > 35000)
{
dis = cost*20/100.0;
amount = cost - dis;
}
}
void display( )
{
System.out.println("Name::" +name);
System.out.println("Mobile No.::" +mobno);
System.out.println("Amount::" +amount);
}
public static void main(String args[])
{
ShowRoom ob = new ShowRoom( );
ob.input( );
ob.calculate( );
ob.display( );
}
}
Output:
EnterName:
Manoj Mahato
Enter Mobile number:
9876543210
Enter cost:
20000
Name::Manoj Mahato
Mobile No.::9876543210
Amount::18000.0
Question 5:
Using the switch-case statement, write a menu-driven program to do the following :
a. To generate and print Letters from A to Z and their Unicode Letters Unicode
Letters | Unicode |
A | 65 |
B | 66 |
– | – |
– | – |
Z | 90 |
b. Display the following pattern using iteration (looping) statement:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
import java.util.*;
class Main
{
public static void main(String args[ ])
{
Scanner sc = new Scanner (System.in);
int i, j,ch;
System.out.println(" 1. Enter 1 for Unicode:");
System.out.println(" 2. Enter 2 for Pattern:");
System.out.println("Enter your choice:");
ch = sc.nextInt();
switch(ch)
{
case 1:
System.out.println( "Letters \t Unicode");
for (i = 65; i <= 90; i++)
{
System.out.println((char)i +"\t" + i);
}
break;
case 2:
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
break;
default:
System.out.println("Wrong choice entered!!!!");
}
}
}
Output:
1. Enter 1 for Unicode:
2. Enter 2 for Pattern:
Enter your choice:
1
Letters Unicode
A 65
B 66
C 67
D 68
- -
- -
Y 89
Z 90
1. Enter 1 for Unicode:
2. Enter 2 for Pattern:
Enter your choice:
2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Question 6: Write a program to input 15 integer elements in an array and sort them in ascending order using the bubble sort technique.
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i, j, t;
int arr[] = new int[15];
System.out.println("Enter 15 integers:");
for (i = 0; i <15; i++)
arr[i] = in.nextInt();
for(i = 0; i < 14; i++)
{
for(j = 0; j < 14 -i; j++)
{
if(arr[j] > arr[j + 1])
{
t = arr[j];
arr[j] = arr [j + 1];
arr[j + 1] = t;
}
}
}
System.out.println("Elements in ascending order are:: ");
for (i = 0; i <15; i++)
System.out.print(arr[i]+" ");
}
}
Output:
Enter 15 integers:
25
36
47
95
21
36
48
45
75
95
62
20
10
96
85
Elements in ascending order are::
10 20 21 25 36 36 45 47 48 62 75 85 95 95 96
Question 7: Design a class to overload a function series() as follows:
(a) void series (int x, int n) – To display the sum of the series given below:
x1 + x2 + x3 + ……………. xn terms
(b) void series (int p) – To display the following series:
0, 7, 26, 63, ……………..p terms.
(c) void series () – To display the sum of the series given below:
Answer:
import java.util.*;
class Main
{
void series( int x, int n)
{
int i;
double a,s;
s = 0;
for (i = 1; i <= n; i++)
{
a = Math.pow(x, i);
s = s + a;
}
System.out.println("Sum::" +s);
}
void series(int p)
{
int i;
for (i = 1; i <= p; i++)
{
System.out.println((i * i * i) - 1 + " ");
}
}
void series()
{
double s,i;
s = 0;
for (i =2; i <= 10; i++)
{
s = s + 1.0/i;
}
System.out.println("Sum:" +s);
}
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int n,x,p;
System.out.println("Enter The Value Of /'x/' and /'n/'");
x=in.nextInt();
n=in.nextInt();
System.out.println("Enter The Value Of /'p/''");
p=in.nextInt();
Main ob = new Main();
ob.series(x,n);
ob.series(p);
ob.series();
}
}
Output:
Enter The Value Of /'x/' and /'n/'
2
3
Enter The Value Of /'p/''
5
Sum::14.0
0
7
26
63
124
Sum:1.928968253968254
Question 8: Write a program to input a sentence and convert it into uppercase and count and display the total number of words starting with a letter ‘A’.
Example:
Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING.
Sample Output: Total number of words starting with letter ‘A’ = 4.
Answer
import java.util.*;
class Main
{
public static void main(String args[ ])
{
int i, c,l;
char ch,ch1;
String st;
Scanner in = new Scanner(System.in);
System.out.println("Enter sentence::");
st = in.nextLine();
st = ' '+ st;
c=0;
l=st.length();
for (i = 0; i <l-1; i++)
{
ch=st.charAt(i);
ch1=st.charAt(i+1);
if((ch == ' ')&&(ch1== 'A'||ch1=='a'))
c++;
}
System.out.println("Total number of words starting with letter /'A/'::" +c);
}
}
Output:
Enter sentence::
ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING
Total number of words starting with letter A::4
Question 9:
A tech number has even number of digits. If the number is split in two equal halves, then the square of sum of these halves is equal to the number itself. Write a program to generate and print all four-digit tech numbers.
Example :
Consider the number 3025
Square of sum of the halves of 3025 = (30+25)2
= (55)2
= 3025 is a tech number.
Answer:
import java.util.*;
class Main
{
public static void main(String args[ ])
{
Scanner in = new Scanner (System.in);
int n,no,d,s,c,a,b,t,p;
System.out.println("Enter The Number");
n=in.nextInt();
no=n;
c=0;
s=0;
while(n>0)
{
n=n/10;
c++;
}
t=c/2;
p=(int)(Math.pow(10,t));
a=no/p;
b=no%p;
s=(int)(Math.pow((a+b),2));
if((s == no) && (c%2 == 0))
System.out.println("Tech Number = " + no);
else
System.out.println("Not Tech Number = " + no);
}
}
Output:
Enter The Number
3025
Tech Number = 3025