Answers Book For Platinum Book
Answers Book For Platinum Book
Answers of MCQ
1. (b) Polymorphism
o Showing different faces.
2. (b) boolean
o Character.isWhitespace(char) returns a boolean value (true or false)
indicating whether the given character is a whitespace.
3. (c) Platform-independent
o The JVM enables Java applications to run on any machine with a JVM
installed, making Java platform-independent.
4. (a) 2 bytes
o The newline character ('\n') is stored as a Unicode character in Java,
which takes 2 bytes.
5. (d) 2.0
o Math.pow(625, 1/2) evaluates to Math.pow(625, 0) = 1, but
Math.sqrt(625) should be used instead.
Math.sqrt(169) - 14 = 13 - 14 = -1.0, and Math.abs(-1) = 1, so 1.0 + 1.0 =
2.0.
6. (c) vcr
o "java".charAt(2) gives 'v', and "script".substring(1, 3) gives "cr", so the
result is "vcr".
7. (a) extends
o The extends keyword is used to inherit a class in Java.
8. (c) 48 bytes
o A float takes 4 bytes. A one-dimensional array of 12 elements takes 12 ×
4 = 48 bytes.
9. (c) 3data
o Variable names cannot start with a digit.
10. (a) 30 20 10
o The loop starts at arr.length - 2 = 2 (value 30) and prints in reverse
order.
11. (b) Supports floating point constants
o The switch statement does not support floating-point values.
12. (d) Syntax
o "10" is a string, not an integer, so int num = "10"; causes a syntax error.
13. (c) The return type, method name, and parameter list
o A method prototype defines these attributes.
14. (a) boolean isEven(int number)
o This is a correct method declaration.
15. (c) (.)dot
o The dot (.) operator accesses class methods.
16. (c) Assertion (A) is true and Reason (R) is false
o The String class is part of java.lang, but not all Java classes belong to
java.lang by default.
17. (a) for(int i = 0; i < 10; i++)
o This is the correct for loop syntax.
18. (b) A constructor is called automatically when an object is created.
Page 1 of 93
o A constructor is invoked when an object is instantiated.
19. (a) (4) (1) (2) (3)
o The variable n must be declared first before using it.
20. (c) Assertion (A) is true and Reason (R) is false
o Method overloading does not depend on return types, only on method
parameters.
Let me know if you need further clarification! �
Here are the answers to the given MCQs:
21. (c) float
Explanation: char is implicitly converted to int, and int is promoted to float in
the expression due to b being float.
22. (b) SimpleInterest
Explanation: A valid class name in Java should not contain spaces, special
characters (except _ or $), or start with a digit.
23. (d) It can be invoked using an object like any other member function.
Explanation: Constructors are automatically called when an object is
instantiated and cannot be explicitly invoked like normal methods.
24. (d) 9.7
Explanation:
o Math.cbrt(27) → 3.0 (cube root of 27)
o Math.sqrt(169) → 13.0
o Math.max(-12.7, -14.5) → -12.7
o Math.min(13.0, -12.7) → -12.7
o 3.0 + (-12.7) = 9.7
25. (c) 109
Explanation:
o s.charAt(1) → 'a' (1st index of "Happy New Year")
o s.lastIndexOf('a') → 109 (ASCII value of 'a' concatenated with
s.lastIndexOf('a'))
26. (c) Manual Memory Management
Explanation: Java has automatic memory management using Garbage
Collection.
27. (a) \"
Explanation: The escape sequence \" is used to include a double quote in a
string.
28. (b) static
Explanation: The static keyword is used to declare class variables (shared
among all objects).
29. (b) new
Explanation: The new keyword is used to instantiate (create) objects from a
class.
30. (c) extends
Explanation: The extends keyword is used for inheritance in Java (to derive a
subclass from a superclass).
Page 2 of 93
32. What is the correct syntax to define a method that does not return a value?
� (c) void
Explanation: The void keyword is used when a method does not return any value.
33. Which of the following is used to refer to the current instance of a class?
� (b) this
Explanation: The this keyword refers to the current instance of the class and is
used to distinguish between instance variables and local variables.
� (b) abstract
Explanation: The abstract keyword is used to declare a method without
implementation. Abstract methods must be implemented by subclasses.
� (b) break
Explanation: The break statement is used to exit a loop prematurely.
� (a) &&
Explanation: The logical AND (&&) has lower precedence than arithmetic operators
(+, *, %).
� (b) final
Explanation: The final keyword makes a variable constant, meaning its value
cannot be changed after assignment.
Page 3 of 93
� (a) inheritance
Explanation: Inheritance is a mechanism in which a class derives properties and
behaviors from another class using the extends keyword.
� (b) --
Explanation: The decrement operator (--) is a unary operator because it operates on
a single operand.
Page 4 of 93
52. b) final
53. c)
54. a) int[] arr;
55. c) 123value (Identifiers cannot start with a digit.)
56. b) new
57. a) void
58. b) == (Used for comparison.)
59. a) final (Prevents inheritance.)
60. a) Method overloading (Same method name with different parameters.)
61. (b) Polymorphism – It allows a class to have multiple forms of a method (method
overloading and method overriding).
62. (d) Abstraction – It hides implementation details and exposes only functionality.
63. (b) Method overriding – This occurs at runtime when a subclass provides a
specific implementation of a method declared in its superclass.
64. (d) Compilation – It is not a pillar of OOP. The four pillars are Inheritance,
Encapsulation, Polymorphism, and Abstraction.
65. (a) Class – A class defines the blueprint for creating objects.
66. (b) Encapsulation – It restricts direct access to an object's attributes and allows
controlled access via methods.
67. (b) Polymorphism – It enables a method to behave differently based on the object
it is acting upon.
68. (d) default – While Java has a default access level, there is no explicit "default"
keyword for access modifiers.
69. (a) Inheritance – One class acquiring the properties and behaviors of another is
called inheritance.
70. (b) && – It is the logical AND operator in Java.
Here are the answers to the questions, along with explanations for each one:
Page 5 of 93
Explanation: The += operator is an assignment operator that adds a value to a
variable and assigns the result back to the variable. The other options like ==,
/, and > are not assignment operators.
Page 6 of 93
Explanation: The && operator is the logical AND operator, which evaluates
whether both conditions are true. Other options like || (logical OR), ! (logical
NOT), and &= (bitwise AND assignment) are not logical AND operators.
89. The if-else statement is used to iterate a block of code multiple times.
False, because if-else is used for decision-making, not iteration. Loops (like for,
while) are used for iteration.
Answer: (b) False
90. The switch statement can work with char, int, and String types in Java.
True, because Java supports char, int, byte, short, enum, and String in
switch statements.
Answer: (a) True
Page 7 of 93
91. The break statement terminates the innermost loop or switch statement.
True, because the break statement stops execution inside the nearest loop or
switch block.
Answer: (a) True
94. The continue statement skips the remaining code in the current iteration of
the loop and proceeds with the next iteration.
True, because continue forces the loop to jump to the next iteration.
Answer: (a) True
Page 8 of 93
i j k (calculated as k += i * j)
8 17 161 + (8 * 17) = 297
16 16 loop exits
Final values:
i = 16, j = 16, k = 297
Answer: (b) i = 16 j = 16 k = 297
Here are the answers along with explanations for each question:
101. Output of the Java program
class array {
static void main() {
int variable[] = new int[10];
for (int i = 0; i < 10; ++i) {
variable[i] = i;
System.out.print(variable[i] + " ");
i++;
}
}
}
Analysis:
The loop initializes i = 0 and increments it by ++i at each iteration.
Inside the loop, variable[i] = i assigns the current i value to variable[i].
System.out.print(variable[i] + " ") prints the value.
i++ at the end of the loop causes an extra increment, effectively skipping every
alternate value.
Output:
02468
Correct Option: (a) 0 2 4 6 8
Page 9 of 93
103. Operators that can operate on a boolean variable
&& (Logical AND) �
== (Equality check) �
?: (Ternary operator) �
+= (Invalid for boolean) �
Correct Option: (d) A, B & C
Page 10 of 93
int marks = name2.equals(name1) ? 50 : 80;
System.out.println("Marks=" + marks);
Since name2.equals(name1) is true, marks = 50.
Output: Marks=50
Correct Option: (b) Marks=50
Page 11 of 93
Correct Option: (a) Actual parameters
122. Which of the following statements about the switch statement is false?
� ( c) The expression in a switch statement can be a long.
Explanation: The switch expression can only be byte, short, char, int, String, or
enum. It cannot be long.
Page 12 of 93
123. Which of the following is true about method overloading in Java?
� ( b) Methods can have the same name but different parameter lists.
Explanation: Method overloading means defining multiple methods with the same
name but with different parameter lists (number, type, or order of parameters).
124. What is the correct way to understand the difference between an identifier and a
keyword in Java?
� ( c) Identifiers are user-defined names, while keywords are reserved words in
Java.
Explanation: Keywords are reserved by Java and cannot be used as variable names,
while identifiers are names given to variables, methods, and classes.
126. Method prototype for the method compute which accepts two integer arguments
and returns true/false:
� ( b) boolean compute(int a, int b)
Explanation:
A method that returns true/false must have the boolean return type.
It should also have proper parameter declarations.
127. Which of the following statements terminates the execution of the current
method and returns control to the calling method?
� ( c) return
Explanation: The return statement exits from the current method and optionally
returns a value.
130. Assertion (A): The Math class can be used in Java programs without importing
any package.
Reason (R): The Math class belongs to the java.lang package, which is automatically
imported by default.
� ( a) Both Assertion (A) and Reason (R) are true and Reason (R) is a correct
explanation of Assertion (A).
Page 13 of 93
Explanation: java.lang is automatically imported in all Java programs, so Math
methods can be used without an explicit import.
131. *A. z = 2;
Explanation: z *= 2; correctly doubles the value of z. The given statement z =* 2;
is incorrect, as the *= operator should be used instead.
132. B. .class
Explanation: When you compile a Java source file (with a .java extension), the
output file generated is a .class file containing the bytecode.
133. B. (3) (4) (1) (2)
Explanation: The correct sequence to swap two variables using a third variable
is:
(3) Declare the third variable (int t = 0;)
(4) Assign the value of a to t (t = a;)
(1) Assign the value of b to a (a = b;)
(2) Assign the value of t to b (b = t;)
134. A. Both Assertion (A) and Reason (R) are true and Reason (R) is a
correct explanation of Assertion (A)
Explanation: void means a method does not return any value, and it is used for
methods that perform actions without returning a result.
135. A. Refers to the current object of a class.
Explanation: The this keyword is used to refer to the current instance (object) of
the class.
136. B. 60
Explanation: The numbers divisible by 20 in the array are 20 and 40. So, their
sum is 60.
137. D. for (int i = 1; ; i++) { }
Explanation: This loop lacks a termination condition, so it will run indefinitely
(infinite loop).
138. B. String
Explanation: String.valueOf(int) returns a String representing the integer value.
139. D. 0 to n-1
Explanation: In Java, array indices start from 0, so the valid index range for an
array of size n is 0 to n-1.
140. B. int multiply(int a, int b)
Explanation: The function prototype should specify the return type first (int),
followed by the method name and its parameters.
141. A. Exits the switch block and continues execution after the switch
Explanation: The break statement in a switch block exits the block and
continues execution after it.
142. B. Both Assertion (A) and Reason (R) are correct, but Reason (R) is
not the correct explanation for Assertion (A).
Explanation: An array is a collection of elements of the same type, but its size
doesn't need to be defined at the time of creation if using dynamic memory in
Java (e.g., using ArrayList).
143. A. do { } while(condition);
Explanation: The correct syntax for a do-while loop is: do { } while (condition);.
144. B. A default constructor will be automatically provided by the
compiler.
Page 14 of 93
Explanation: If no constructor is explicitly defined, the Java compiler provides a
default no-argument constructor.
145. B.
Explanation: The correct way to calculate the sum of the first n natural
numbers is by starting the loop from 1 and going up to n (inclusive).
146. A. Both Assertion (A) and Reason (R) are correct, and Reason (R) is
the correct explanation for Assertion (A).
Explanation: A function prototype in C or Java provides the function name,
return type, and parameters, and allows for type checking.
147. B. Polymorphism
149. A. boolean
Explanation: The isDigit(char) function returns a boolean indicating whether the
character is a digit or not.
150. C. Conditional
Explanation: The ?: operator is the ternary (conditional) operator in Java, which
evaluates a condition and returns one of two values based on whether the condition is
true or false.
Here are the answers with explanations for the questions:
152. The correct term for converting an object to its corresponding data type is:
Answer: B. Unboxing
Explanation: Unboxing refers to converting an object to a corresponding data type.
155. A member variable declared with a access specifier has visibility only in
the:
Answer: B. class
Explanation: A variable with the access modifier is only visible within the class in
which it is declared. It cannot be accessed directly outside of that class.
Page 15 of 93
Explanation: In Java, the new keyword is used to dynamically allocate memory for
objects.
159. Expressions that have all operands of the same type are called:
Answer: A. Pure
Explanation: Expressions where all operands are of the same type are often referred to
as "pure" expressions. If operands of different types are mixed, it would be a "mixed"
expression.
160. Arrange the following data types in descending order of their size:
Answer: B. double, float, short, byte
Explanation: In Java, the size of the primitive data types is as follows:
double is 8 bytes.
float is 4 bytes.
short is 2 bytes.
byte is 1 byte.
162. The access specifier that gives the least accessibility is:
Answer: B.
Explanation: The access modifier restricts access to the member only within the
same class. It provides the least accessibility compared to and protected.
Page 16 of 93
Answer: C. 0.25
Explanation:
a[1] is 2 and a[4] is -2.
Math.pow(2, -2) equals 0.25 (2 raised to the power of -2).
Math.ceil(0.25) returns 1.0, so the output is 1.0.
Page 17 of 93
System.out.println(arr);
� d. Garbage value
Explanation: When printing an array directly, Java prints the memory reference
instead of its elements.
177. Assuming int takes 4 bytes of memory, what is the size of int arr[15]?
� d. 60 bytes
Explanation:
Each int takes 4 bytes, so an array of 15 integers will occupy:
15 × 4 = 60 bytes
Page 18 of 93
180. We can determine the length of an array using
� d. array.length
Explanation: The length property of an array returns the number of elements in it.
Example:
int arr[] = new int[5];
System.out.println(arr.length); // Output: 5
Here are the correct answers with explanations:
181. Write a valid statement to declare a single-dimensional array of 5 integers.
� Answer: d. None of these
Explanation:
The correct way to declare an array of 5 integers in Java is:
int arr[] = new int[5];
None of the given options are correct because:
int arr[5] = new int[5]; is incorrect syntax in Java.
int arr[6] = new int[6]; has an incorrect size.
int arr[4] = new int[4]; has an incorrect size.
182. What are the starting row and column indices of the array m[3][4]?
� Answer: a. 0 and 0 respectively
Explanation:
In Java, arrays use zero-based indexing.
A 3×4 array means 3 rows and 4 columns.
The first row index is 0, and the first column index is 0.
184. From the given array y[][] = {{3,4,5},{6,7,8},{1,2,3}};, the element at y[2][2] is:
� Answer: c. 3
Explanation:
y[2][2] refers to row index 2 and column index 2, which corresponds to 3 in the
array.
185. Write a set of statements that would find the size of the array ar[] =
{2,5,6,7,8,9,10};
� Answer: a. int len = ar.length; System.out.println(“The size of array =”+len);
Explanation:
length is a property of arrays in Java, not a method.
ar.length(); and ar.len; are incorrect syntax.
Page 19 of 93
187. How many times will the given loop execute?
� Answer: b. 9
Explanation:
The loop:
for(int k=1024; k>1; k=k/2)
Executes as:
1024 → 512 → 256 → 128 → 64 → 32 → 16 → 8 → 4 → 2
Total iterations: 9 (before k becomes 1 and the condition k > 1 fails).
193. The String method, which can result in a negative integer, is:
� Answer: c. compareTo
Explanation:
compareTo() returns a negative integer if the first string is lexicographically
smaller.
Page 20 of 93
194. Which data type can be used to store the value 123456789?
� Answer: c. long
Explanation:
int has a max value of 2,147,483,647, which is fine, but for larger values, long
is preferred.
201. The parameters that are passed to the method when it is invoked are called:
b. actual parameters
� Actual parameters are the values passed to a function/method at the time of calling
it. Formal parameters are the variables used to receive these values inside the method.
Page 21 of 93
� “string Hello " consists of "string" (6 letters) + " " (space 1 letter)+ "Hello" (5 letters) +
" " (space 1 letter) → Total 13 characters.
209. The second last element of an array of size 100 has index:
c. 98
� Indices range from 0 to 99. The second last element is at 98.
211. Maximum number of objects that can be created from a single Java class:
d. None
� There is no predefined limit on object creation in Java.
Page 22 of 93
212. Creating an object from a class is called:
b. Instantiating
� Instantiation means creating an object from a class using new.
216. To make members of class Employee accessible only within the class:
a.
� The access specifier restricts access to only within the class.
Page 23 of 93
display() is a method in the product class, an instance (ob) can invoke it using
ob.display();.
222.
Answer: d. (ii)
Explanation:
_var is valid because variable names can start with an underscore _.
var.name is invalid because . is not allowed in variable names.
*var is invalid because * is not a valid character for variable names.
4var is invalid because variable names cannot start with a digit.
223.
Answer: d. (ii), (iii), (i), (iv)
Explanation: The order of data type precedence in Java (highest to lowest) is:
double > float > long > int
224.
Answer: b. 25.0
Explanation:
a = 6.25, so
a * Math.pow(10,2) = 6.25 * 100 = 625
Math.sqrt(625) = 25.0
225.
Answer: a. Call by reference
Explanation: In call by reference, changes made in the function affect the original
argument.
226.
Answer: a. static
Explanation: The static keyword allows methods and variables to be accessed without
creating an object.
227.
Answer: a. JIT compiler
Explanation: The Just-In-Time (JIT) compiler converts bytecode into native machine
code for better performance.
228.
Answer: b. impure methods
Explanation: Impure methods modify the state of an object, whereas pure methods do
not change state.
229.
Answer: a. continue
Explanation: The continue statement skips the remaining loop body and jumps to the
next iteration.
230.
Page 24 of 93
Answer: b. 21
Explanation:
For x = 3:
Expression: (x++ + --x) * x-- + ++x
Breakdown:
1. x++ = 3 (Post-increment, so x becomes 4)
2. --x = 3 (Pre-decrement, so x becomes 3)
3. (3 + 3) = 6
4. x-- = 3 (Post-decrement, x becomes 2)
5. ++x = 3 (Pre-increment, x becomes 3)
Final result: 6 * 3 + 3 = 18 + 3 = 21
231.
Answer: c. Hello Brother How do you do?
I am good Explanation:
System.out.print() does not add a new line.
System.out.println() moves to the next line after printing.
Thus, output:
Hello Brother How do you do?
I am good
232.
Answer: b. default
Honour
Explanation:
x = 'D', so it goes to default: case.
The default case does not have a break, so execution falls through to case 'C'.
Honour is printed on next line.
233.
Answer: b. Loop is executed 2 times and the output is 12
Explanation:
Loop values:
1. a = 6 → 6 % 4 != 0, so continue
2. a = 12 → 12 % 4 == 0, so break
Output: 12
234.
Answer: c. java.lang
Explanation: Wrapper classes (Integer, Double, etc.) are in java.lang package.
235.
Answer: c. java.lang
(Same as above.)
236.
Answer: b. isLetter()
Explanation: isLetter(char c) checks if the character is a letter.
237.
Page 25 of 93
Answer: b. parseInteger()
Explanation: There is no parseInteger() method in Java. The correct method is
Integer.parseInt().
238.
Answer: d. Java.lang
Explanation: String functions are in java.lang.String.
239.
Answer: d. static
Explanation: A static variable belongs to the class, not instances.
240.
Answer: c. . dot
Explanation: . is not considered a punctuator, in java it is considered as an
operator(access operator).
Here are the answers with explanations:
Page 26 of 93
2. + (addition), - (subtraction)
Among these, % and /has the highest precedence in the given expression.
248. Order of precedence in the expression: a > d && b || !c
� b. > ! && ||
Explanation: Operator precedence:
1. > (relational)
2. ! (logical NOT)
3. && (logical AND)
4. || (logical OR)
249. Which keyword is used to declare a class?
� c. class
Explanation: The class keyword is used to define a class.
250. What keyword is used to inherit a class?
� b. extends
Explanation: The extends keyword is used for class inheritance.
251. Output of System.out.println(Math.abs(-5) + Math.pow(4, 1/2));
� b. 6.0
Explanation:
Math.abs(-5) → 5
Math.pow(4, 1/2) calculates 4^0, which is 1.0 (since 1/2 is integer division, it
becomes 0).
5 + 1.0 = 6.0
252. Output of System.out.println(Math.max(5, Math.min(3, 7)) +
Math.round(2.6));
� b. 8
Explanation:
Math.min(3, 7) → 3
Math.max(5, 3) → 5
Math.round(2.6) → 3
5+3=8
253. Which method returns a boolean?
� a. equalsIgnoreCase(String)
Explanation: The equalsIgnoreCase(String) method compares two strings while
ignoring case differences and returns a boolean.
254. Which type is not allowed in a switch statement?
� c. double
Explanation: switch does not support floating-point types (double and float).
255. Which of the following is true about the switch statement?
� d. Case labels must be compile-time constants.
Explanation: case labels must be constant expressions, not variables.
256. What happens if a switch statement does not have a break after a case?
� c. The next case will be executed (fall-through).
Explanation: Without break, execution continues into the next case.
257. Which of the following statements is true about switch cases?
� d. Case labels are unique.
Explanation: Each case must have a unique constant value.
258. Correct way to declare a method?
� a. void methodName()
Explanation: The correct syntax is void methodName() { }.
Page 27 of 93
259. Output of the given Java program:
� a. 15
Explanation: The sum method returns 5 + 10 = 15.
260. Which of the following is true about method overloading?
� b. Methods can have the same name but different parameter lists.
Explanation: Method overloading allows multiple methods with the same name but
different parameters (type, number, or both).
Here are the correct answers with explanations:
262. What is the correct way to understand the difference between an identifier
and a keyword?
Answer: (c) Identifiers are user-defined names, while keywords are reserved words in
Java.
Explanation: Identifiers are names given to variables, classes, and methods. Keywords
are predefined and cannot be used as identifiers.
264. Which of the following statements terminates the execution of the current
method and returns control to the calling method?
Answer: (c) return
Explanation: The return statement exits the current method and passes control back
to the caller.
266. Which of the following statements will immediately terminate the entire
program?
Answer: (d) System.exit(0)
Explanation: System.exit(0) stops the entire Java application.
Page 28 of 93
267. The default value of a char variable is:
Answer: (d) '\u0000'
Explanation: The default value of char in Java is the null character '\u0000', which
represents an empty character.
271. Assertion (A): The Math class can be used in programs without importing
any package.
Answer: (a) Both Assertion (A) and Reason (R) are true, and Reason (R) is a correct
explanation of Assertion (A).
Explanation: The Math class belongs to the java.lang package, which is automatically
imported.
272. A developer wants to subtract 5 from a variable y. What is the correct way
to write this statement?
Answer: (d) Both A and C
Explanation: The correct ways are y -= 5; and y = y - 5;. The original y =- 5; is
incorrect.
273. __________ and __________ are the numeric wrapper classes in Java.
Answer: (c) Byte and Short
Explanation: Numeric wrapper classes in Java include Byte, Short, Integer, Long,
Float, and Double.
Page 29 of 93
277. If the element 0 of an array holds a float, the rest of the pockets will be of
type:
Answer: (b) float only
Explanation: Arrays in Java are homogeneous, meaning all elements must be of the
same type.
278. Which among the following is/are the correct way(s) to create an integer
array of size 40?
Answer: (b) int []a=new int[40]
Explanation: Java does not allow negative or range-based array size declarations.
279. The second last element of an array of size 100 has index:
Answer: (c) 98
Explanation: Array indices start from 0, so the second last element is at index 98 (100
- 2).
280. Given array int marr[ ] = {101, 202, 303, 404}; the value of marr[-1+2] is:
Answer: (b) 202
Explanation: marr[-1+2] simplifies to marr[1], which is 202.
Page 30 of 93
285. Correct statement to add 10 to a:
� (d) Both A and C
Explanation:
a += 10; is shorthand for a = a + 10;, both are correct.
a =+ 10; is incorrect (interpreted as a = (+10)).
Page 31 of 93
294. Accessing element in 2D array at second row and third column
� (b) arr[1][2]
� Explanation: Indexing starts from 0, so:
First row: arr[0]
Second row: arr[1]
Third column: arr[1][2]
Page 32 of 93
307. (c) 12
Math.min(-35.83, -143.83) is -143.83, Math.abs(-143.83) is 143.83, Math.ceil(143.83) is
144, Math.sqrt(144) is 12.
308. (b) Coercion
Implicit type conversion (coercion) occurs when a lower type is converted to a higher type
automatically.
309. (a) //
Single-line comments in Java are denoted by //.
310. (d) Any number
A class can be used to create multiple objects in Java.
311. (c) Unboxing
Unboxing is converting a wrapper object to its corresponding primitive type.
312. (a) Parameter list
Constructor overloading is differentiated by different parameter lists.
313. (b) 40
The constructor sets t = 40, so when System.out.println(t1.t); executes, it prints 40.
314. (d) int a = Integer.parseInt(s);
Integer.parseInt("123") converts a string to an integer.
315. (b) java.lang
Wrapper classes belong to java.lang, which is imported by default.
316. (a) Autoboxing
Autoboxing is converting a primitive value into an object of its corresponding wrapper
class.
317. (d) isWhitespace()
Character.isWhitespace() checks if a character is a whitespace, including tab spaces.
318. (b) default
default is a reserved keyword in Java.
319. (c) To exit the method and optionally pass back a value
The return keyword exits a method and may return a value.
320. (c) 0675
Octal literals in Java start with 0, and 0675 is a valid octal number.
Page 33 of 93
1. a = a++ + ++b → a = 2 + 4 = 6, then a becomes 3, but after assignment, it is 6.
2. b = ++a - b-- → b = 7 - 3 = 2.
Final values: a = 7, b = 2.
327. Method prototype for check() returning char with an int parameter?
Answer: (d) char check(int x)
Explanation: The correct syntax includes the return type, function name, and
parameter list.
Page 34 of 93
Answer: (a) Autoboxing
Explanation: Autoboxing converts a primitive (int) to an object (Integer).
Page 35 of 93
i runs 2 times.
j runs from -1 to 2 (4 times).
2 * 4 = 8 times in total.
Page 36 of 93
353. + 354 is a single question Ans: (a) 150
The loop adds all even numbers: 10 + 20 + 30 + 40+50 = 150.
355. (b) 6
arr[3] + arr[1] = 4 + 2 = 6.
356. (c) 20
arr[arr.length - 2] = arr[3] = 20.
357. (a) 55
Squaring each element and summing: 1² + 2² + 3² + 4² + 5² = 1 + 4 + 9 + 16 +
25 = 55.
358. (a) 4
Each element is divided by 2: arr[3] = 8 / 2 = 4.
359. (a) 40
arr[4] - arr[0] = 50 - 10 = 40.
360 & 361 is a single question (d) 6
arr[3] = 3 * 2 = 6
362. **(c) 8 times *, 7 times +++++
The loop alternates between "***" (odd) and "+++++" (even).
363. (a) for ( int i = 99; i >= 0; i / 9 )
i / 9 is incorrect, as it doesn't change i, causing an infinite loop.
364. (b) 2
case 6: y=0; falls through to case 7: y=1; and then default: y+=1; making y = 2.
365. (a) 1 3 5 7
The loop prints odd numbers and skips multiples of x=2. It stops at 8.
366. (c) double
The highest data type in the expression (double) dominates.
Page 37 of 93
int p = 7, q = 4;
q += --p * q++ - p--;
System.out.println("p = " + p);
System.out.println("q = " + q);
Step-by-step evaluation:
1. p = 7, q = 4
2. --p → p = 6
3. q++ → q = 4 (post-increment, so q becomes 5 after use)
4. p-- → p = 6 (post-decrement, so p becomes 5 after use)
5. q += (6 * 4 - 6) → q = 4 + (24 - 6) = 22
6. p was post-decremented, so p = 5
Output:
p=5
q = 22
Page 38 of 93
System.out.println("x = " + x);
System.out.println("y = " + y);
Step-by-step evaluation:
1. x = 4, y = 7
2. --y → y = 6
3. x++ → x = 4 (post-increment, so x becomes 5 after use)
4. ++x → x = 6
5. y-- → y = 6 (post-decrement, so y becomes 5 after use)
6. x = 4 - 6 + 6 + 6 = 10
Output:
x = 10
y=5
Page 39 of 93
switch(number)
{
case 0: System.out.println("Zero"); break;
case -1: System.out.println("Negative"); break;
case 1: System.out.println("Positive"); break;
}
Error: switch cannot handle ranges.
Corrected Code:
int number = 5;
if (number > 0)
System.out.println("Positive");
else if (number < 0)
System.out.println("Negative");
else
System.out.println("Zero");
Page 40 of 93
11. String Methods
String str1 = "Programming", str2 = "Program";
(a)
System.out.println(str1.substring(0, str2.length()));
str2.length() is 7, so str1.substring(0, 7) gives "Program"
Output:
Program
(b)
System.out.println(str2.toUpperCase().toLowerCase());
str2.toUpperCase() converts "Program" to "PROGRAM"
.toLowerCase() converts it back to "program"
Output:
program
Page 41 of 93
Learn
(b)
System.out.println(phrase.length());
" Learn " has 11 characters.
Output:
11
Page 42 of 93
System.out.println(msg.indexOf('i') + msg.lastIndexOf('i'));
"Coding in "
o indexOf('i') = 3 (first occurrence)
o lastIndexOf('i') = 9 (last occurrence)
Output:
12
Page 43 of 93
String s = "ICSE2024";
System.out.println(s.substring(2, 5).toUpperCase());
System.out.println(s.indexOf('2'));
s.substring(2, 5) extracts "SE2"
.toUpperCase() doesn't change anything since it's already uppercase → "SE2"
s.indexOf('2') finds the first occurrence of '2' in "ICSE2024" → index 4
Output:
SE2
4
Page 44 of 93
{ z
Page 45 of 93
Result is: 16
Page 46 of 93
System.out.println("Area: " + (length * breadth));
}
}
(i) What is constructor overloading? Is it used in the above class? Explain.
Constructor overloading allows multiple constructors with different
parameters.
Yes, it is used in the Rectangle class:
o Rectangle() initializes length and breadth to 1.
o Rectangle(int l, int b) initializes them with user-specified values.
(ii) What will be the area of the rectangle if Rectangle(4, 5) is called?
length = 4, breadth = 5
Area = 4 × 5 = 20
Output:
Area: 20
Page 47 of 93
(i) Name the constructors used in the Box class.
Default constructor (Box())
Parameterized constructor (Box(int l, int b, int h))
(ii) Predict the output when Box(2, 3, 4) is called and volume() is invoked.
length = 2, breadth = 3, height = 4
Volume = 2 × 3 × 4 = 24
Output:
Volume: 24
Page 48 of 93
}
System.out.print("OK " + (++x));
Page 49 of 93
(ii) Having more than one constructor allows constructor overloading, enabling
object creation with default values or user-defined values.
Page 50 of 93
int x = 1;
do
{
System.out.print(x);
x++;
} while (x <= 10);
Page 51 of 93
(a) Length of array: 5
(b) Value at z[3]: 20
(c) Output of System.out.println(z[1] * 2 + z[4]);
10 * 2 + 25 = 45
(d) Reversed array: {25, 20, 15, 10, 5}
New z[0] value: 25
Page 52 of 93
48. Loop Execution
(i) Output:
10
15
20
25
30
(ii) Output:
1
4
(iii) Output:
2
4
6
8
10
(iv) Output:
0
2
4
(v) Output:
7
11
15
19
49. Output
Oceanis
true
Page 53 of 93
case 'h':
case 'H': System.out.print("HINDI"); break;
default: System.out.print("PHYSICAL EDUCATION");
}
Q63. How many types of programs can be made in Java? Define them.
1. Standalone Applications - Executed independently on a user's machine.
2. Applets - Small programs embedded in web pages.
3. Web Applications - Java Servlets and JSPs for web development.
4. Enterprise Applications - Large-scale applications using Java EE.
5. Mobile Applications - Android apps using Java.
6. Embedded Systems - Java used in IoT and smart devices.
Page 54 of 93
Q66. Java feature that executes multiple operations simultaneously:
Multithreading
Java feature where lowercase and uppercase letters are different: Case Sensitivity
Page 55 of 93
44
4.5 4
55
5.5 6
66
6.5 6
77
Number of Iterations: 13
Q72. Answers:
1. Loop that executes even if the condition is false: do-while loop.
2. Access right to access superclass data in subclass of any package: protected
Q75. Answers:
Unary Logical Operator: !
Feature that makes variables visible: Scope
Page 56 of 93
void func() {
int localVar = 5; // Local
}
class Test {
static void main( ) {
Example e1 = new Example();
Example e2 = new Example();
System.out.println(e2.objVar); // Output: 20
System.out.println(e2.classVar); // Output: 50
}
}
82. Keywords
i) return
ii) this
iii) Method or Function (Encapsulation of reusable statements)
Page 57 of 93
// Selection Sort first finds the smallest (11) and swaps with first element.
Page 58 of 93
93. Index in Java
An index represents the position of a data item in an array.
Numbering: Starts from 0 in Java.
Page 59 of 93
y -= 5;
}
return y;
}
Dry Run
x = 10, y = 30
x = 11, y = 25
x = 12, y = 20
x = 13, y = 15
x = 14, y = 10 (Loop stops)
Loop executes 4 times.
Returns 10.
Section - B
Answers of following questions (programs)
class CabService
{
char type;
double km, bill;
// Constructor
CabService() {
type = ' ';
km = 0.0;
bill = 0.0;
}
// Accept method
void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter car type (A for AC, N for NON AC): ");
type = sc.next().charAt(0);
System.out.print("Enter kilometers traveled: ");
km = sc.nextDouble();
}
// Calculate method
void calculate()
{
if (type == 'A')
{
bill = (km <= 5)
bill =150
else
bill =150 + (km - 5) * 10;
Page 60 of 93
}
else if (type == 'N')
{
bill = (km <= 5)
bill =120
bill =120 + (km - 5) * 8;
}
}
// Display method
void display()
{
System.out.println("CAR TYPE: " + type);
System.out.println("KILOMETER TRAVELLED: " + km);
System.out.println("TOTAL BILL: Rs. " + bill);
}
// Constructor
Student()
{
rollnum = 0;
name = "";
marks = 0;
grade = "";
}
// Accept method
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter roll number: ");
rollnum = sc.nextInt();
sc.nextLine(); // Consume newline
System.out.print("Enter name: ");
name = sc.nextLine();
System.out.print("Enter marks: ");
Page 61 of 93
marks = sc.nextInt();
}
// Compute grade
void compute() {
if (marks > 90) grade = "A";
else if (marks >= 75) grade = "B";
else if (marks >= 60) grade = "C";
else if (marks >= 40) grade = "D";
else grade = "E";
}
// Display method
void display()
{
System.out.println("Roll Number\tName\tMarks\tGrade");
System.out.println(rollnum + "\t\t" + name + "\t" + marks + "\t" + grade);
}
P3: ATransport
import java.util.*;
class ATransport
{
String name;
int w, charge;
// Constructor
ATransport()
{
name = "";
w = 0;
charge = 0;
}
// Accept method
void accept( )
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter customer name: ");
name = sc.nextLine();
System.out.print("Enter weight of parcel (kg): ");
w = sc.nextInt();
Page 62 of 93
}
// Calculate charge
void calculate( )
{
if (w <= 10)
charge = w * 25;
else if (w <= 30)
charge = (10 * 25) + ((w - 10) * 20);
else
charge = (10 * 25) + (20 * 20) + ((w - 30) * 10);
// Adding 5% surcharge
charge += (charge * 5) / 100;
}
// Print method
void print( )
{
System.out.println("Name\tWeight\tBill Amount");
System.out.println(name + "\t" + w + "\tRs. " + charge);
}
Page 63 of 93
bas = sc.nextInt();
System.out.print("Enter years of experience: ");
expn = sc.nextInt();
}
// Calculate increment
void increment( )
{
if (expn < 3)
inc = 1000 + 0.1 * bas;
else if (expn < 5)
inc = 3000 + 0.12 * bas;
else if (expn < 10)
inc = 5000 + 0.15 * bas;
else
inc = 8000 + 0.2 * bas;
nbas = bas + inc;
}
// Display method
void display( )
{
System.out.println("Name: " + name);
System.out.println("Basic Salary: Rs. " + bas);
System.out.println("Experience: " + expn + " years");
System.out.println("Increment: Rs. " + inc);
System.out.println("New Basic Salary: Rs. " + nbas);
}
Page 64 of 93
units = sc.nextInt();
}
// Calculate bill
void calculate( )
{
if (units <= 100)
billAmt = units * 5;
else if (units <= 300)
billAmt = (100 * 5) + ((units - 100) * 7);
else
billAmt = (100 * 5) + (200 * 7) + ((units - 300) * 10);
}
// Display method
void display( )
{
System.out.println("Name\tUnits\tBill Amount");
System.out.println(name + "\t" + units + "\tRs. " + billAmt);
}
static void main( )
{
ElectricityBill bill = new ElectricityBill();
bill.accept();
bill.calculate();
bill.display();
}
}
Page 65 of 93
{
DA = BasicSalary * 0.53;
TA = BasicSalary * 0.12;
HRA = BasicSalary * 0.10;
PF = BasicSalary * 0.08;
}
else if (BasicSalary >= 10000)
{
DA = BasicSalary * 0.45;
TA = BasicSalary * 0.10;
HRA = BasicSalary * 0.12;
PF = BasicSalary * 0.075;
}
else
{
DA = BasicSalary * 0.40;
TA = BasicSalary * 0.08;
HRA = BasicSalary * 0.14;
PF = BasicSalary * 0.07;
}
GrossSalary = (BasicSalary + DA + TA + HRA) - PF;
}
void display( )
{
System.out.println("EMPLOYEE No.\tNAME\tGROSS SALARY");
System.out.println(Emp_No + "\t\t" + Name + "\t" + GrossSalary);
}
static void main( )
{
Empl e = new Empl();
e.get();
e.calcu();
e.display();
}
}
Page 66 of 93
System.out.println("Enter names:");
for (int i = 0; i < n; i++)
names[i] = sc.nextLine();
// Bubble Sort
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (names[j].compareTo(names[j + 1]) > 0)
{
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}
System.out.println("Sorted Names:");
for (int i = 0; i < n; i++)
System.out.println(name);
}
}
Page 67 of 93
System.out.println("Sorted Words (Descending Order):");
for (int i = 0; i <40; i++)
System.out.println(word);
}
}
if (found==1)
System.out.println("Search Unsuccessful, Name not enlisted.");
else
System.out.println("Name: " + names[i] + ", Phone: " + phoneNumbers[i]);
}
}
Page 68 of 93
for (int i = 1; i < 10; i++)
{
if (arr[i] < min) {
min = arr[i];
minIndex = i;
}
if (arr[i] > max)
{
max = arr[i];
maxIndex = i;
}
}
System.out.println("Smallest Number: " + min + " at index " + minIndex);
System.out.println("Largest Number: " + max + " at index " + maxIndex);
}
}
System.out.println("Modified Array:");
for (int i = 0; i < 10; i++)
System.out.print(arr[i] + " ");
}
}
Page 69 of 93
class ReverseArray
{
static void main( )
{
Scanner sc = new Scanner(System.in);
int arr[] = new int[10];
System.out.println("Enter 10 numbers:");
for (int i = 0; i < 10; i++)
arr[i] = sc.nextInt();
// Reversing the array
for (int i = 0; i < 5; i++)
{
int temp = arr[i];
arr[i] = arr[9 - i];
arr[9 - i] = temp;
}
System.out.println("Reversed Array:");
for (int i = 0; i < 10; i++)
System.out.print(num + " ");
}
}
System.out.println("Original Array:");
for (int i = 0; i < 20; i++)
System.out.print(arr[i] + " ");
System.out.println("\nEven Numbers:");
for (int i = 0; i < eIndex; i++)
System.out.print(even[i] + " ");
Page 70 of 93
System.out.println("\nOdd Numbers:");
for (int i = 0; i < oIndex; i++)
System.out.print(odd[i] + " ");
}
}
Page 71 of 93
// Input state to search
System.out.print("Enter the state to search: ");
String key = sc.nextLine();
class NameSearch {
static void main( ) {
String Names[] = {"Alex", "Brian", "Charlie", "David", "Ethan", "Frank", "George",
"Jack"};
Scanner sc = new Scanner(System.in);
Page 72 of 93
else
low = mid + 1;
}
if (found != -1)
System.out.println("Search successful");
else
System.out.println("Search element not found");
}
}
Page 73 of 93
String y=name.substring(1,l-1);
char z=name.charAt(l-1);
System.out.print(x+”. ”+y+”. “+z);
}
}
class ReverseCase
{
static void main( )
{
String newstr=””;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
int l=str.length();
for (int i = 0; i <l; i++)
{
char x=name.charAt(0);
if (Character.isUpperCase(ch))
newstr=newstr+Character.toLowerCase(ch);
else if (Character.isLowerCase(ch))
newstr=newstr+Character.toUpperCase(ch);
else
newstr=newstr+ch;
}
}
}
Page 74 of 93
// Display the modified string
System.out.println("Output: " + result);
}
}
P20: Shift Vowels and Consonants to Their Next Letter
import java.util.*;
class ShiftCharacters
{
static void main( )
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
String vowels = "aeiouAEIOU";
String nextVowel = "eiouaEIOUA";
for (int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if(Character.isLetter(c))
{
if (c=‟a‟)
newstr=newstr+‟e‟;
else if (c=‟e‟)
newstr=newstr+‟i‟;
else if (c=‟i‟)
newstr=newstr+‟o‟;
else if (c=‟o‟)
newstr=newstr+‟u‟;
else if (c=‟A‟)
newstr=newstr+‟E‟;
else if (c=‟E‟)
newstr=newstr+‟I‟;
else if (c=‟I‟)
newstr=newstr+‟O‟;
else if (c=‟O‟)
newstr=newstr+‟U‟;
else if (ch == 'z')
newstr=newstr+‟b‟;
else if (ch == 'Z')
newstr=newstr+‟B‟;
else
newstr=newstr+ (char) (ch + 1); // Shift consonant to next letter
}
else
newstr=newstr+c;
}
System.out.print(newstr);
}
}
Page 75 of 93
P21: Count Double Letter Sequences
import java.util.*;
class CountDoubleLetters
{
static void main( )
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine().toUpperCase();
int count = 0;
for (int i = 0; i < str.length()-1; i++)
{
if (str.charAt(i) == str.charAt(i + 1) && Character.isLetter(str.charAt(i)))
count++;
}
System.out.println("Output: " + count);
}
}
class InitialsWithDots
{
static void main( )
{
String newstr=””;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
str=” “+str;
for (int i = 0; i <words.length; i++)
{
char c=str.charAt(i);
if(c=‟ „)
{
char c1=str.charAt(i+1);
newstr=newstr+ Character.toUpperCase( c1)+”.”;
}
}
System.out.print(newstr);
}
}
Page 76 of 93
class Consecutive
{
static void main( )
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine().toUpperCase();
for (int i = 0; i < str.length() - 1; i++)
{
char c=str.charAt(i);
char c1=str.charAt(i + 1);
if(c==c1)
System.out.println(c+”,”+c1);
}
}
System.out.println("Input Matrix:");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
System.out.print(matrix[i][j] + "\t");
System.out.println();
}
Page 77 of 93
System.out.println("Sum of odd numbers: " + oddSum);
}
}
System.out.println("Input Matrix:");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
System.out.print(matrix[i][j] + "\t");
System.out.println();
}
Page 78 of 93
int[][] matrix = new int[3][4];
System.out.println("Enter elements of 3×4 matrix:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++) {
matrix[i][j] = sc.nextInt();
}
System.out.print("Enter number to search: ");
int num = sc.nextInt();
boolean found = false;
System.out.println("Sum of matrices:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
Page 79 of 93
{
C[i][j] = A[i][j] + B[i][j];
System.out.print(C[i][j] + "\t");
}
System.out.println();
}
}
}
Page 80 of 93
{
static void main( )
{
Scanner sc = new Scanner(System.in);
int[][] matrix = new int[4][4];
int[] colSum = new int[4];
System.out.println("Enter elements of 4×4 matrix:");
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
matrix[i][j] = sc.nextInt();
colSum[j] += matrix[i][j];
}
Page 81 of 93
void main( )
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int temp = num, sum = 0, length = String.valueOf(num).length();
while (temp > 0)
{
int digit = temp % 10;
sum += Math.pow(digit, length);
temp /= 10;
length--;
}
if (sum == num)
System.out.println(num + " is a Disarium number.");
else
System.out.println(num + " is not a Disarium number.");
}
}
Page 82 of 93
P33: Product of Successors of Even Digits
import java.util.*;
class SuccessorProduct
{
void main( )
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int product = 1, count = 0;
while (num > 0)
{
int digit = num % 10;
if (digit % 2 == 0)
{
product *= (digit + 1);
count++;
}
num /= 10;
}
if (count == 0)
product = 0; // No even digits case
System.out.println("Output: " + product);
}
}
Page 83 of 93
P35:
class OverloadCompare
{
// Compare number of digits
void compare(int a, int b)
{
int digitsA = String.valueOf(a).length();
int digitsB = String.valueOf(b).length();
if (digitsA > digitsB)
System.out.println(a);
else if (digitsB > digitsA)
System.out.println(b);
else
System.out.println("Numbers are equal");
}
P36:
class OverloadHLine
{
// Print triangle with '*'
void hline(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= (2 * i - 1); j++)
{
System.out.print("* ");
}
System.out.println();
}
Page 84 of 93
}
P37:
class OverloadSeries
{
// Single argument series: 1 + 1/2 + 1/3 + ... + 1/n
double series(double n)
{
double sum = 0;
for (double i = 1; i <= n; i++)
{
sum += 1 / i;
}
return sum;
}
P38:
class OverloadDisplay
Page 85 of 93
{
// Print pattern
void display()
{
for (int i = 5; i >= 1; i--)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j + "\t");
}
System.out.println();
}
}
switch (choice)
{
Page 86 of 93
case 'S':
double SI = (P * R * T) / 100;
System.out.println("Simple Interest: " + SI);
break;
case 'C':
System.out.print("Enter number of times interest is compounded per year:
");
int n = sc.nextInt();
double CI = P * Math.pow((1 + R / (100 * n)), n * T) - P;
System.out.println("Compound Interest: " + CI);
break;
default:
System.out.println("Invalid Choice!");
}
}
}
switch (choice)
{
case 'A':
int sum = 0;
for (int i = 1; i < num; i++)
{
if (num % i == 0)
sum += i;
}
if(sum > num)
System.out.println(num +" is an Abundant Number")
else
System.out.println(num +" is NOT an Abundant Number"));
break;
case 'B':
int c=0;
while (num > 0)
{
int digit = num % 10;
Page 87 of 93
if(digit==0)
c++;
num /= 10;
}
If(c>1)
System.out.println(“It is a Duck Number”);
else
System.out.println(“It is not a Duck Number”);
}
break;
default:
System.out.println("Invalid choice!");
}
}
}
Page 88 of 93
P42: Courier Charges Calculation
import java.util.Scanner;
class Courier
{
public static void main( )
{
double charge = 0;
if (wt <= 0)
charge = 0;
else if (wt <= 100 && type == 'O')
charge = 80;
else if (wt <= 100 && type == 'E')
charge = 100;
else if (wt <= 500 && type == 'O')
charge = 150;
else if (wt <= 500 && type == 'E')
charge = 200;
else if (wt <= 1000 && type == 'O')
charge = 210;
else if (wt <= 1000 && type == 'E')
charge = 250;
else if (wt > 1000 && type == 'O')
charge = 250;
else if (wt > 1000 && type == 'E')
charge = 300;
class P43 {
static void main( ) {
Scanner sc = new Scanner(System.in);
System.out.println("Choose an option:\n1. Fibonacci Series\n2. Sum of Digits");
int choice = sc.nextInt();
Page 89 of 93
switch (choice)
{
case 1:
int a = 0, b = 1, c;
System.out.print(a + " " + b + " ");
for (int i = 3; i <=10; i++)
{
c = a + b;
System.out.print(c + " ");
a = b;
b = c;
}
System.out.println();
break;
case 2:
System.out.print("Enter a number: ");
int num = sc.nextInt(), sum = 0;
while (num > 0)
{
sum += num % 10;
num /= 10;
}
System.out.println("Sum of digits: " + sum);
break;
default:
System.out.println("Invalid choice!");
}
}
}
void Stron(String s)
{
for (int i = s.length(); i > 0; i--)
System.out.println(s.substring(0, i));
}
Page 90 of 93
void Stron(String s, int n)
{
System.out.println(s.substring(0, n));
}
void main( )
{
Stron obj = new Stron();
obj.Stron("OKAY", 'A');
obj.Stron("OKAY");
obj.Stron("VIBGYOR", 3);
}
}
}
}
class Pellfib
{
void main( )
{
Scanner sc = new Scanner(System.in);
Page 91 of 93
System.out.println("Choose an option:\n1. Fibonacci Sum\n2. Pell Sum");
int choice = sc.nextInt();
System.out.print("Enter number of terms: ");
int n = sc.nextInt();
int sum = 0;
switch (choice)
{
case 1:
int a = 0, b = 1, c;
for (int i = 0; i < n; i++)
{
sum += a;
c = a + b;
a = b;
b = c;
}
break;
case 2:
int x = 0, y = 1, z;
for (int i = 0; i < n; i++)
{
sum += x;
z = 2 * y + x;
x = y;
y = z;
}
break;
default:
System.out.println("Invalid choice!");
}
System.out.println("Sum of series: " + sum);
}
}
class Palin
{
void main( )
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = sc.next();String rev=””;
for(int i=0;i<s.length();i++)
{
char c=sc.charAt(i);
Page 92 of 93
rev=c+rev;
}
if (s.equalsIgnoreCase(rev))
System.out.println(s.toLowerCase());
else
System.out.println(s.toUpperCase());
}
}
import java.util.*;
class Binary
{
void main( )
{
double[] arr = {4.1, 7.8, 12.6, 18.3, 25.0, 33.7, 41.9, 50.5, 67.4, 88.1};
Scanner sc = new Scanner(System.in);
System.out.print("Enter a decimal number to search: ");
double key = sc.nextDouble();
int low = 0;
Page 93 of 93