Q1.
MCQ
1. are special symbols that define the structure of a program
Answer: b) Separator
2. Escape sequence are always preceded by
Answer: c) \
3. They may begin with an alphabet, underscore or dollar sign
Answer: c) Identifiers
4. An identifier should never begin with
Answer: b) number
5. An identifier should not have in between.
Answer: a) Space
6. An identifier should not have in between.
Answer: a) Full stop
7. These words cannot be used as an identifier
Answer: a) Reserved
8. Decimal, octal and hexadecimal are types of constants
Answer: a) integer
9. integer have combinations of digits 0 through 7 with leading zero
Answer: a) Octal
10. Notation has three parts. Integer, decimal point and fractional.
Answer: b) Decimal
11. is a real number expressed in decimal notation or an integer.
Answer: c) Literals
12. A number having part in a floating point number.
Answer: c) exponent
13. does not change its value during execution of the program
Answer: a) Literals
14. constants can take only two values, true or false
Answer: a) Boolean
15. constants are enclosed within single quotes
Answer: b) char
16. constant are enclosed within double quotes
Answer: c) String
17. What is other name of implicit conversations?
Answer: a) Widening
18. What is other name of explicit conversations?
Answer: b) Narrowing
CH-5
19. An operator may appear as prefix or postfix
Answer: a) Unary
20. operators and other operators combine to build shorthand operators
Answer: c) Assignment
21. operators that are used to compare the objects are
Answer: b) Relational
22. operators return only true or false
Answer: b) Relational
23. The order in which operators are applied is known as
Answer: a) Precedence
24. The order in which the operators are executed is known as
Answer: b) Associativity
25. In print(), after printing cursor will be there in line
Answer: a) Same
26. In println(), after printing cursor will be there in line
Answer: b) Next
27. Evaluate the value of the expression 6 − 2 + 10\% 4 + 7
10\%4 = 2
6−2=4
4+2=6
6 + 7 = 13
Answer: c) 13
28.
```java
int x = 0, y = 0, z = 0; q =
(++x + y--) * z++;
```
· ++x → x=1
· y-- → y=0 (use 0, then y becomes -1)
· (1 + 0) = 1
· z++ → z=0 (use 0, then z becomes 1)
·1*0=0
Answer: b) 0
29. Increment operator increases value of variable by
Answer: b) 1
CH-6
30. The Scanner class is a class in
Answer: b) [Link]
31. These errors are also called compile time errors
Answer: a) Syntax
32. These errors check grammar of the statements
Answer: a) Syntax
33. are not included in program and are ignored by the compiler
Answer: b) Comments
34. int i = 10; int n = i++ % 5;
· i++ → use 10, then i becomes 11
· 10 % 5 = 0
Answer: c) 0
35. If a = 10 and b = 20, what will be value of a when a += b is executed?
a = 10 + 20 = 30
Answer: c) 30
36. Which type of values can be input using nextFloat()?
Answer: b) Real
37. Each statement in java must end with
Answer: b) Semicolon
38. Which among the following is not a logical operator?
Answer: c) >=
39. Which among the following is not a primitive data type?
Answer: c) String
CH-7
40. [Link]([Link]([Link](-6.33)));
[Link](-6.33) = -6.0
[Link](-6.0) = 6.0
Answer: c) 6.0
41. [Link]([Link]([Link](26.77)));
[Link](26.77) = 27
[Link](27) = 27.0
Answer: a) 27.0
42. [Link](98.7) returns this value
Answer: a) 99
43. How to express pi in java expression?
Answer: b) [Link]
44. Which of the following is false to find square of a number?
Answer: c) [Link](a,2)
CH-8
45. statement is a two-way branch statement
Answer: d) if
46. statement is a multi-way branch statement
Answer: c) switch
47. The operator that is short cut to if-else statement
Answer: c) Ternary
48. Test expression is a condition that returns a ___ value
Answer: c) Boolean
49. statement is used to terminate the statement sequence.
Answer: c) break
50. for, while and do while are statements.
Answer: a) iteration
51. is an entry controlled loop
Answer: a) for
52. An loop is created in case of for (;;)
Answer: b) infinite
53. Which of the following is an exit controlled loop?
Answer: b) do- while
54. The method used to find total number of characters in a string.
Answer: b) length()
55. It converts the string into capital letters
Answer: a) toUpperCase()
56. It converts the string into small letters
Answer: d) toLowerCase()
57. Instead of a = a + 1 we can also write
Answer: b) ++a
58. Instead of a = a + 5 we can also write
Answer: b) a += 5
59. While execution, multiplication has ___ precedence then addition
Answer: a) Higher
CH-9
60. When the statements are repeated sequentially several times in a program, the
construct is known as:
Answer: a) iteration
61. Which of the following loop does not execute even once if the condition is false in
the beginning?
Answer: b) while
62. Which of the following loop checks the condition first and then execution begins?
Answer: b) while loop
63. How many times the loop, for (i = 1; i++), will execute, if there is no statement to
terminate the loop?
Answer: b) infinite
64. Which of the following cases uses a case called default?
Answer: b) switch
65. Which of the following is an iterative construct in Java?
Answer: b) for
66. Which keyword is used to exit a loop in Java?
Answer: b) break
67. Which of the following is NOT a type of loop in Java?
Answer: c) repeat-until
68. Which loop in Java will always execute its body at least once?
Answer: c) do-while loop
Q-1: Java Expressions
1. Z = ∛(4x + y)
→ double Z = [Link](4 * x + y);
2. C = |a + b|
→ double C = [Link](a + b);
3. C = (3a² + b²) / (√a + √b)
→ double C = (3 * a * a + b * b) / ([Link](a) + [Link](b));
4. Z = (x + 3y²)²
→ double Z = [Link](x + 3 * y * y, 2);
5. A = p(1 + r/100)^n
→ double A = p * [Link](1 + r / 100.0, n);
6. Z = √((x + y) / (x - y))
→ double Z = [Link]((x + y) / (x - y));
7. C = (1/3)a³ + (1/4)b³
→ double C = (1.0 / 3.0) * a * a * a + (1.0 / 4.0) * b * b * b;
8. Z = √(5x² + y) / √(x + y²)
→ double Z = [Link](5 * x * x + y) / [Link](x + y * y);
9. Z = ax⁵ + bx⁷ + c³
→ double Z = a * [Link](x, 5) + b * [Link](x, 7) + [Link](c, 3);
10. C = (a + b)^n + √3
→ double C = [Link](a + b, n) + [Link](3);
Java Evaluation
1. int x = 10, y = 20;
z = (++x + --y) + y-- + +++x + 23 % 3 - 5 + x++;
11 + 19 + 19 + 12+ 2 - 5 + 12 = 70
z = 70
2. int d = 20;
e = d++ * 3 + d++ + d-- + d-- / 4;
20 * 3 + 21 + 22 + 5 = 108
e = 108
3. int x = 10, y = 4;
z = y-- + x-- + (++x * 2) + +++y + --y + x++ + --x;
4 + 10 +( 10 * 2) + 4 + 3+ 10 + 10 = 61
z = 61
4. int a = 7, b = 5;
c = (a++ + ++b) + (b++ * 2) + a++ % 2 - 5;
7 + 6 + 12 + 8 % 2 -5 → 20
c = 20
5. int x = 6, y = 3;
z = ++y + ++x * 2 + --x * 3 - ++x * 4 + 100 + y
→4+ 7 * 2+ 6 * 3+ 7 * 4 +100 + 4 = 112
z = 112
6. int x = 10;
y = (++x + x++) * 5 + x-- + --x * 2;
→ (11 + 11 ) * 5 + 12 + 20 = 142
y = 142
7. int y = 4;
z = (y++ * 2) + ++y + 25 / 4 + y++ - 10;- y++ * 2
→ 4*2 + 6 - 25 / 4 + 6 -10 → 16
z = 16
8. int a = 4, b = 5;
c = (++a + b++) * 4 + a++ + +++b - ++b;
→ (5 + 5) * 4 + 5 + 7 - 8 = 44
c = 44
9. int m = 2, n = 4;
p = ++n + n++ - ++m * m++ + ++49 / 2;
→ 5 + 5 - 3 * 3 + 25 = 26
p = 26
10. int x = 5, y = 10;
z = (y++ * 4) + x++ % 2 + y++ + ++x - y-- + x--;
→ 10*4 + 1 + 11 + 7 - 12 + 7 = 54
z = 54
Math function
1.i. [Link](1, 25) → 1.0
ii. [Link](1764) → 42.0
2.i. [Link](33.33) → 33
ii. [Link](-66) → 66
3.i. [Link](87, 78) → 87
ii. [Link](87, 78) → 78
4.i. [Link](807.16) → 808.0
ii. [Link](807.16) → 807.0
[Link](64) → 4.0
[Link](16) → 4.0
[Link] a[][] = new int[5][5]; → Rows = 5, Columns = 5
[Link] m[] = {9,18,27,36}; → [Link] → 4
[Link] x[] = {2,4,6,8}; → x[3] → 8
[Link] a[][] = {{3,6},{9,12}}; → a[1][1] → 12
[Link] x[][] = new int[4][3]; → Rows = 4, Columns = 3
String logical question:
1. What will be the output of the following code?
String a = “Windows”;
[Link]([Link]());
[Link]([Link](0));
i. [Link]() → 7
ii. [Link](0) → 'W'
2. What will be the output of the following code?
String x = “magnetic field”;
[Link]([Link]());
[Link]([Link](3));
i. [Link]() → 14(includes space)
ii. [Link](3) → 'n'
3. What will be the output of the following code?
String m = “environment”;
[Link]([Link]());
[Link]([Link]());
i. [Link]() → 11
ii. [Link]() → ENVIRONMENT
4. What will be the output of the following code?
String a = “TOKENS”;
[Link]([Link](4));
[Link]([Link]());
i. [Link](4) → Compile error — should be charAt(4) → 'N'
ii. [Link]() → tokens
5. What will be the output of the following code?
String x = “TECHNOLOGY”;
[Link]([Link]());
[Link]([Link](6));
i. [Link]() → TECHNOLOGY
ii. [Link](6) → 'L’
[Link] will be the output of the following code?
String x= “NESTED LOOPS”;
[Link]([Link](8));
[Link]([Link]());
i. [Link](8) → 'O'
ii. [Link]() → 12
7. What will be the output of the following code?
String x = “magnetic field”;
[Link]([Link]());
[Link]([Link](3));
i. [Link]() → 14
ii. [Link](3) → 'n'
8. What will be the output of the following code?
String a = “TOKENS”;
[Link]([Link](4));
[Link]([Link]());
i. [Link](4) → charAt(4) → 'N'
ii. [Link]() → tokens
9. What will be the output of the following code?
String x = “World CUP”;
[Link]([Link]());
[Link]([Link](6));
i. [Link]() → WORLD CUP
ii. [Link](6) → 'C'
10. What will be the output of the following code?
String x = “SAMSUNG”;
[Link]([Link]());
[Link]([Link]());
i. [Link]() → samsung
ii. [Link]() → 7
Question and Answer
1. What is data type? What different data types are available in Java?
Data type is defined as the set of possible values a variable can hold. They are
1. Primitive data type (Integers, floating point numbers, character and Boolean)
2. Non Primitive data type (String, Array)
2. State the difference between identifier and keywords?
• Identifiers :
➢ Identifiers are the name given to the variables or methods
➢ e.g. sum, mark, rno
• Keywords
➢ Keywords are reserve words with special purpose
➢ e.g. public, class, for
3. What is type conversion?
The process of converting one primitive data type to another is called type conversion.
1. Implicit conversion or widening or coercion
2. Explicit conversion or narrowing or type casting
4. What are the types of literals in Java?
1. Integer
2. Real
3. Character
4. String
5. Boolean
5. What are operators in java?
Operators are the mathematical symbol used to perform mathematical and logical operations among two or
more operands.
e.g.
Arithmetic operator
Relational operator
Logical operator
Assignment operator
6. What are arithmetic operators? Give examples.
Arithmetic operators are used to perform simple arithmetic operations on primitive data types.
They are
1. + Addition
2. - Subtraction
3. * Multiplication
4. / Division
5. % Modulus
7. What is unary operator? What is the different between unary and
binary?
Unary operators require only one operand. It can appear before
argument (prefix) or after argument (postfix).
A unary operator needs one operand where as a binary operator needs
two operands.
8. What are relational operators?
Relational operators are used to compare two or more operands.
They are
1. > greater than
2. < less than
3. >= greater than equal to
4. <= less than equal to
5. == equal to
6. != not equal to
9. What is the function of conditional operator?
A conditional operator provides a shorter syntax for if….then….else
statement. It is called ternary operator “?:” and has three operands.
Syntax:
boolean expression? value1 : value2;
e.g.
int max=(a>b)?a:b;
10. What is ternary operator? Give example.
It is only conditional operator that takes three operands. It is shortcut to if – else statement.
Syntax: (condition) ? value1 : value2;
Eg. int max = (a>b) ? a : b;
11. What is a block?
A block is a group of statements that form a single compound statement.
12. What is the use of break statement?
break statement causes immediate exit from the loop. It can be used in switch and all looping statements.
13. What are jump statements in java?
Jump statements are used to interrupt the normal flow of the program. The control jumps to another section of
program.
14. What do you mean by infinite loop?
Infinite loop occurs in the case of invalid condition. (when for loop is empty or loop condition in while and do
while is false)
eg. for ( ; ; )
15. What are the Java packages? List any 2 packages available in Java.
Java packages are collections of related classes and interfaces that organize code and avoid naming conflicts.
1. [Link]
2. [Link]
3. [Link]
16. How do you refer to a class in a package? Give a simple example.
To refer to a class in a package, import the package.
Example:Using import
import [Link];
Scanner scanner = new Scanner([Link]);
17. What are single-line comments and multi-line comments? Give suitable examples.
➢ Single-line comment: //
Example:
// This is a single-line comment
➢ Multi-line comment: /* ... */
Example:
/*
This is a
multi-line comment
*/
18. What is an error? List 3 types of errors.
➢ Syntax Errors :are the errors that occur when we violate the rules of writing the statements of the
programming language. Eg int sum (missing semicolon)
➢ Runtime error: Errors that occur during the execution of the program primarily due to the state of the
program which can only be resolved at runtime are called Run Time errors.
Consider the below example:Eg:dividing any number by zero is run time error
➢ Logical error :When a program compiles and runs without errors but produces an incorrect result, this type of
error is known as logical error.
PROGRAMS
/*Q1 (a) Sum of a Geometric Series
Java program to compute the sum of the series Sum = 5 + 25 + 125 + .............nterms:
Sum of Alternating Factorial Series
(b)Java program to compute the sum S = a - (a/2!) + (a/3!) - (a/4!) + .............nterms:*/
import [Link];
public class Series1
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter n ");
int n = [Link]();
double sum = 0;
for (int i = 1; i <= n; i++)
{
sum =sum+ [Link](5, i);
}
[Link](" sum = " + sum);
}
}
import [Link];
public class Series
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a: ");
double a = [Link]();
[Link]("Enter n: ");
int n = [Link]();
double sum = 0.0;
double f = 1.0;
for (int i = 1; i <= n; i++)
{
f=1;
for(int j=1;j<=i;j++)
{
f=f*j;
}
if(i%2==0)
sum = sum-(double) (a/f);
else
sum = sum+(double) (a/f);
}
[Link]("Sum = " + sum);
}
}
/*Q2 (a) – Sum of a Decreasing Series (10 + 9 + 8 + … + 1)
Java program to find and display the sum of the series:
(b) – Sum of a Geometric Series (k/2 + k/4 + k/8 + … +nterms)
Java program to input k and n and compute the sum of the series:*/
public class series2
{
public static void main(String[] args)
{
int n = 10;
int sum = 0;
for (int i = n; i >= 1; i--)
{
sum = sum+i;
}
[Link]("Sum = " + sum);
}
}
import [Link];
public class SeriesSum
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter k ");
int k = [Link]();
[Link]("Enter n ");
int n = [Link]();
double sum = 0.0;
for (int i = 1; i <= n; i++)
{
sum = sum + (double)k/ [Link](2, i);
}
[Link]("Sum = " + sum);
}}
/*Q3(a) Sum of Harmonic Series 1+1/2+1/3+1/4.........1/n
* (b) Sum of First n Even Numbers */
import [Link];
public class HarmonicSeries
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter n: ");
int n = [Link]();
double sum = 0.0;
for (int i = 1; i <= n; i++)
{
sum = sum+(double) 1.0 / i;
}
[Link]("Sum = " + sum);
}}
import [Link];
public class SumEven
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter n: ");
int n = [Link]();
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum =sum + i*2;
}
[Link]("Sum = " + sum);
}
}
/*Q4 (a) Java program to display the Fibonacci series for the first *n terms:
* Example usage:
Input: n = 7
Output: 0 1 1 2 3 5 8
(b) Java program to find the sum of the alternating series *1 - 2 + 3 - 4 + ... ± n:* */
import [Link];
public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of terms: ");
int n = [Link]();
int a = 0, b = 1,c=0;
[Link]("Fibonacci Series: " + a + " " + b);
for (int i = 3; i <= n; i++)
{
c = a + b;
[Link](" " + c);
a = b;
b = c;
}
[Link]();
}
}
import [Link];
public class Series
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the value of n: ");
int n = [Link]();
double sum = 0;
for (int i = 1; i <= n; i++)
{
if (i % 2 == 0)
sum = sum - i;
else
sum = sum + i;
}
[Link]("Sum of the series: " + sum);
}
}
/*Q5
(a) Sum of a series
Write a program to input a number a and find the sum of the series:
S = a^2 + a^2/2 + a^2/3 + ...........+a^2/10.
(b) Sum of series involving factorials
Write a program to input a number n and compute the sum of the series:
S = (1+2)/(1*2) + (1+2+3)/(1*2*3) + .................+(1+2+3+...+n)/(1*2*3*...*n).*/
import [Link];
public class SeriesSumA
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a: ");
double a = [Link]();
double sum = 0;
for (int i = 1; i <= 10; i++)
{
sum=sum+ (a * a) / i;
}
[Link]("Sum = " + sum);
}
}
import [Link];
public class SeriesSumFactorial
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter n: ");
int n = [Link]();
double s = 0,f=1,sum=0;
for (int i = 2; i <= n; i++)
{
s = 0;
f=1;
for(int j=1;j<=i;j++)
{
f=f*j;
s=s+j;
}
sum=sum+(double)s/f;
}
[Link]("Sum = " + sum);
}
}
/*Q6 Prime Numberis a number that has exactly two factors: 1 and itself.
Example: 7 is a prime number because it is only divisible by 1 and 7.*/
import [Link];
public class PrimeCheck
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int count = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
count = count + 1;
}
}
if (count == 2)
[Link](n + " is a Prime number.");
else
[Link](n + " is NOT a Prime number.");
}
}
/*Q7 Niven Number is a number that is divisible by the sum of its digits.
Example: 18 is a Niven number because 1 + 8 = 9 and 18 is divisible by 9.*/
import [Link];
public class NivenCheck
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int sum = 0;
int temp = n;
for (int t = temp; t > 0; t = t / 10)
{
int d = t % 10;
sum = sum + d;
}
if (n % sum == 0)
[Link](n + " is a Niven number.");
else
[Link](n + " is NOT a Niven number.");
}
}
/*Q8 Armstrong Number is a number in which the sum of cubes of its digits is equal to the number itself.
Example: 153 is an Armstrong number because 1³ + 5³ + 3³ = 153.*/
import [Link];
public class ArmstrongCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int sum = 0;
int temp = n;
for (int t = temp; t > 0; t = t / 10)
{
int d = t % 10;
sum = sum + (d * d * d);
}
if (sum == n)
[Link](n + " is an Armstrong number.");
else
[Link](n + " is NOT an Armstrong number.");
}
}
/*Q9 Palindrome Number is a number that remains the same when its digits are reversed.
Example: 121 is a palindrome number.*/
import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int rev = 0;
int temp = n;
for (int t = temp; t > 0; t = t / 10)
{
rev = rev * 10 + (t % 10);
}
if (rev == n)
[Link](n + " is a Palindrome number.");
else
[Link](n + " is NOT a Palindrome number.");
}
}
/*Q10 Spy number is a number where the sum of its digits is equal to the product of its digits.
Example: 123 is a spy number because 1 + 2 + 3 = 6 and 1 * 2 * 3 = 6.*/
import [Link];
public class SpyCheck
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int sum = 0, prod = 1;
int temp = n;
for (int t = temp; t > 0; t = t / 10)
{
int d = t % 10;
sum = sum + d;
prod = prod * d;
}
if (sum == prod)
[Link](n + " is a Spy number.");
else
[Link](n + " is NOT a Spy number.");
}}
/*Q11. Write a menu-driven program in Java using the Scanner class to perform the following operations
based on user's choice:Addition,Subtraction,Multiplication,Division*/
import [Link];
public class Calculator
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("1. Add");
[Link]("2. Subtract");
[Link]("3. Multiply");
[Link]("4. Divide");
[Link]("Enter your choice (1-4): ");
int choice = [Link]();
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
if (choice == 1)
[Link]("Sum = " + (a + b));
else if (choice == 2)
[Link]("Difference = " + (a - b));
else if (choice == 3)
[Link]("Product = " + (a * b));
else if (choice == 4)
{
if (b != 0)
[Link]("Quotient = " + (a / b));
else
[Link]("Cannot divide by zero");
}
else
[Link]("Invalid choice");
}}
/*Q12 Write a menu-driven program in Java using the Scanner class to:
Calculate simple interest
Calculate the area of a rectangle*/
import [Link];
public class InterestAndArea
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("1. Calculate Simple Interest");
[Link]("2. Calculate Area of Rectangle");
[Link]("Enter your choice (1-2): ");
int choice = [Link]();
if (choice == 1)
{
[Link]("Enter Principal: ");
double p = [Link]();
[Link]("Enter Rate: ");
double r = [Link]();
[Link]("Enter Time: ");
double t = [Link]();
double si = (p * r * t) / 100;
[Link]("Simple Interest = " + si);
}
else if (choice == 2)
{
[Link]("Enter length: ");
int l = [Link](); [Link]("Enter breadth: ");
int b = [Link]();
[Link]("Area = " + (l * b));
}
else
[Link]("Invalid choice");
}
}
/*Q13 Write a menu-driven program in Java using the Scanner class to:
Check whether a number is even or odd
Check whether a number is positive or negative*/
import [Link];
public class NumberCheck
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("1. Check Even or Odd");
[Link]("2. Check Positive or Negative");
[Link]("Enter your choice (1-2): ");
int choice = [Link]();
[Link]("Enter a number: ");
int num = [Link]();
if (choice == 1)
{
if (num % 2 == 0)
[Link]("Even number");
else
[Link]("Odd number");
}
else if (choice == 2)
{
if (num >= 0)
[Link]("Positive number");
else
[Link]("Negative number");
}
else
[Link]("Invalid choice");
}
}
/*Q14 Write a menu-driven program in Java using the Scanner class to:
Print the multiplication table of a number Print numbers from 1 to N*/
import [Link];
public class TableOrCount
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("1. Print Multiplication Table");
[Link]("2. Print numbers from 1 to N");
[Link]("Enter your choice (1-2): ");
int choice = [Link]();
if (choice == 1) { [Link]("Enter number: ");
int n = [Link]();
for (int i = 1; i <= 10; i++)
{
[Link](n + " x " + i + " = " + (n * i));
}
}
else if (choice == 2)
{
[Link]("Enter N: ");
int n = [Link]();
for (int i = 1; i <= n; i++) { [Link](i);
}
} else
[Link]("Invalid choice");
}
}
/*15. Write a menu-driven program in Java using the Scanner class to:
Convert a string to uppercase
Count the number of characters in the string*/
import [Link];
public class StringOperations
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("1. Convert to Uppercase");
[Link]("2. Find String Length");
[Link]("Enter your choice (1-2): ");
int choice = [Link]();
[Link]();
[Link]("Enter a string: ");
String str = [Link]();
if (choice == 1)
[Link]("Uppercase: " + [Link]());
else if (choice == 2)
[Link]("Length: " + [Link]());
else
[Link]("Invalid choice");
}
}
/*Q16Design a class LibraryBill to calculate the amount to be paid by a student for the
* books issued from a library the detail of the class are given below
class name :LibraryBill
data members
String name - to store name of the student
int days - to store number of days the book are kept
double fine - to store total fine amount
member methods
void accept(): to input name of the student & number of days the book are kept using scanner class
void calcute(): to compute fine according to
1 - 7days ---> no fine
8 - 15days ---> rs 2 per day
above 15days ---> rs 5 per day
void display(): to display name of the student,number of days,total fine amount*/
import [Link];
class LibraryBill
{
String name;
int days;
double fine;
void accept()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter student name: ");
name = [Link]();
[Link]("Enter number of days the book was kept: ");
days = [Link]();
}
void calculate()
{
if (days <= 7)
fine = 0.0;
else if (days >= 8 && days <= 15)
fine = 7*0.0+(days - 7) * 2.0;
else
fine = 7*0.0+ 8 * 2.0 + (days - 15) * 5.0;
}
void display()
{
[Link]("Student Name: " + name);
[Link]("Number of days: " + days);
[Link]("Total fine amount: " + fine);
}
public static void main(String[] args)
{
LibraryBill obj = new LibraryBill();
[Link]();
[Link]();
[Link]();
}
}
/*Q17 Design a class touristfare to calculate the fare charged by a tourist cabservice
class name : TouristFare
data members
String name - name of the customer
int km - Distance travelled in kilometer
double fare - total fare amount
member methods
void accept(): to input customer name & kilometer using scanner class
void calcute(): to compute fare according to
1 - 50Km ---> rs 15 per km
51 - 150Km ---> rs 12 per km
above 150km ---> rs 10 per km
void display(): to display details
*/
import [Link];
class TouristFare
{
String name;
int km;
double fare;
void accept()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter customer name: ");
name = [Link]();
[Link]("Enter distance travelled in kilometers: ");
km = [Link]();
}
void calculate()
{
if (km <= 50)
fare = km * 15.0;
else if (km>50 && km<=150)
fare = 50*15.0 + (km-50)* 12.0;
else
fare =50*15.0 + 100* 12.0 + (km-150) * 10.0;
}
void display()
{
[Link]("Customer Name: " + name);
[Link]("Distance Travelled: " + km );
[Link]("Total Fare " + fare);
}
public static void main(String[] args)
{
TouristFare obj = new TouristFare();
[Link]();
[Link]();
[Link]();
}
}
/*[Link] a class Hotel with the following description:
Member variables:
String name – to store the name of the customer
long mno – to store the mobile number of the customer
double bill – to store the bill amount
double gst – to store GST amount
double st – to store the service tax
double tamt – to store the total amount to be paid by the customer
Member methods:
void accept() – to accept customer’s name, mobile number, bill.
void calculate() – to calculate GST, service tax and total amount to be paid by the customer.
gst = 18% on bill st = 12.5% on bill tamt = bill +gst +st
void display() – to display the customer’s name, mobile number and bill amount.
Write a main method to create an object and invoke the above member methods.*/
import [Link].*;
class Hotel {
String name;
long mno;
double bill;
double gst;
double st;
double tamt;
void accept()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter customer name: ");
name = [Link]();
[Link]("Enter mobile number: ");
mno = [Link]();
[Link]("Enter bill amount: ");
bill = [Link]();
}
void calculate()
{
gst = 0.18 * bill;
st = 0.125 * bill;
tamt = bill + gst + st;
}
void display()
{
[Link]("Customer Name: " + name);
[Link]("Mobile Number: " + mno);
[Link]("Total Bill: " + tamt);
}
public static void main(String[] args)
{
Hotel obj = new Hotel();
[Link]();
[Link]();
[Link]();
}
}
Q19 Area of square,rectangle and triangle
import [Link];
class Area
{
double compute(double s)
{
return s * s;
}
double compute(int l, int b)
{
return l * b;
}
double compute(double base, double height)
{
return 0.5 * base * height;
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
Area obj = new Area();
int choice;
double result;
[Link]("Area Calculation Menu:");
[Link]("1. Calculate area of Square");
[Link]("2. Calculate area of Rectangle");
[Link]("3. Calculate area of Triangle");
[Link]("4. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
switch (choice)
{
case 1:
[Link]("Enter side of square: ");
double s = [Link]();
result = [Link](s);
[Link]("Area of square = " + result);
break;
case 2:
[Link]("Enter length of rectangle: ");
double l = [Link]();
[Link]("Enter breadth of rectangle: ");
double b = [Link]();
result = [Link](l, b);
[Link]("Area of rectangle = " + result);
break;
case 3:
[Link]("Enter base of triangle: ");
double base = [Link]();
[Link]("Enter height of triangle: ");
double height = [Link]();
result = [Link](base, height);
[Link]("Area of triangle = " + result);
break;
case 4:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice! Try again.");
} }}
Q20 Volume of cube,area of triangle,perimeter of square
import [Link];
class MathOverload
{
double result;
void compute(int a)
{
result = a * a * a; // cube of a number
}
void compute(int b, int h)
{
result = 0.5 * b * h; // area of a triangle
}
void compute(double s) {
result = 4.0 * s; // perimeter of a square
}
void display()
{
[Link]("Result = " + result);
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
MathOverload obj = new MathOverload();
[Link]("Enter a number to find its cube: ");
int a = [Link]();
[Link](a);
[Link]();
[Link]("Enter b ");
int b = [Link]();
[Link]("Enter h ");
int h= [Link]();
[Link](b, h);
[Link]();
[Link]("Enter side of square: ");
double side = [Link]();
[Link](side);
[Link]();
}
}
Common Variable Description Box for All Programs
Data Type Variable Name Description
int To store input number, limit, or count
int Sum To store sum of values
int i, j, t Loop variables
int temp Temporary storage for calculations
int rev To store reversed number
int choice To store menu choice
int a, b To store two numbers for operations
int P, ,t Principal, rate, time (for interest)
int ,b Length, breadth (for area)
int km Kilometers travelled
int days Number of days
int fact Factorial value
int term Current term in series
double fare Total fare amount
double fine Fine amount
double bill, gst, st Bill, GST, service tax
double total, tamt Total amount
double si, area, result Simple interest, area, result of computation
String name To store name of customer/student
String S To store a string
long mno To store mobile number
Scanner SC Scanner object for input