0% found this document useful (0 votes)
18 views17 pages

Java Assertions and Answers Guide

Uploaded by

macroff9932.ac
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views17 pages

Java Assertions and Answers Guide

Uploaded by

macroff9932.ac
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Objective type Question Answer

Class – X , ICSE
Prepared & Guided By : Souvik Chakraborty , [Link](C.S) , [Link]

1. Basics

Q1.

Assertion (A): Java is platform-independent.


Reason (R): Java code is compiled to bytecode, which can run on any platform
with a JVM.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q2.

Assertion (A): Java supports multiple inheritance using classes.


Reason (R): Java provides interfaces to achieve multiple inheritance.

Answer:

 A is false, R is true.
(Explanation: Java does not support multiple inheritance with classes
directly, but it can be achieved through interfaces.)

Q3.

Assertion (A): Java uses garbage collection to manage memory.


Reason (R): The [Link]() method guarantees the execution of the garbage
collector.

Answer:

By : Souvik_Chakraborty_M.Sc_CS 1/17
 A is true, R is false.
(Explanation: The [Link]() method only suggests the JVM to run the
garbage collector; it does not guarantee it.)

Q4.

Assertion (A): Java does not support operator overloading.


Reason (R): Java’s simplicity is maintained by restricting operator overloading.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q5.

Assertion (A): Java’s main() method must be declared public.


Reason (R): The main() method is the entry point for the JVM to start program
execution.

Answer:

 A is true, R is true, and R is the correct explanation of A.

2. Loops

Q1.

Assertion (A): The while loop is an entry-controlled loop.


Reason (R): The condition is checked before the loop body is executed in a while
loop.

Answer:

 A is true, R is true, and R is the correct explanation of A.

By : Souvik_Chakraborty_M.Sc_CS 2/17
Q2.

Assertion (A): The for loop is useful for iterating over an array.
Reason (R): The for loop can have an initialization, condition, and
increment/decrement in a single line.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q3.

Assertion (A): A break statement terminates the nearest enclosing loop.


Reason (R): The break statement stops loop execution and transfers control to the
statement after the loop.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q4.

Assertion (A): The continue statement in a loop skips the current iteration.
Reason (R): The continue statement transfers control to the beginning of the loop.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q5.

Assertion (A): A do-while loop checks the condition at the start of the loop.
Reason (R): A do-while loop is suitable for executing the body at least once
regardless of the condition.

Answer:
By : Souvik_Chakraborty_M.Sc_CS 3/17
 A is false, R is true.
(Explanation: The condition in a do-while loop is checked after the loop
body runs.)

3. Arrays

Q1.

Assertion (A): The elements of an array are stored in contiguous memory


locations.
Reason (R): Accessing elements of an array using indices ensures fast retrieval.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q2.

Assertion (A): The length of an array can be changed after its declaration.
Reason (R): Arrays in Java are fixed in size once created.

Answer:

 A is false, R is true.
(Explanation: Arrays in Java have a fixed size, and their length cannot be
modified after declaration.)

Q3.

Assertion (A): The ArrayIndexOutOfBoundsException is thrown if an invalid


index is accessed.
Reason (R): Java checks array bounds at runtime to ensure memory safety.

Answer:

 A is true, R is true, and R is the correct explanation of A.


By : Souvik_Chakraborty_M.Sc_CS 4/17
Q4.

Assertion (A): Arrays can store both primitive types and objects in Java.
Reason (R): Arrays are a part of Java’s collection framework.

Answer:

 A is true, R is false.
(Explanation: Arrays are not a part of the Java collection framework; they
are part of the core language.)

Q5.

Assertion (A): An array can be passed as an argument to a method.


Reason (R): Passing an array to a method sends a reference to the original array.

Answer:

 A is true, R is true, and R is the correct explanation of A.

4. Strings

Q1.

Assertion (A): Strings in Java are stored in the heap memory.


Reason (R): The String class is immutable, and string literals are stored in the
string pool.

Answer:

 A is true, R is true, but R is not the correct explanation of A.

By : Souvik_Chakraborty_M.Sc_CS 5/17
Q2.

Assertion (A): The == operator checks the reference equality of strings.


Reason (R): The equals() method in Java checks the content equality of strings.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q3.

Assertion (A): The StringBuilder class is preferred for string concatenation in a


loop.
Reason (R): The StringBuilder class is mutable and avoids creating multiple
objects.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q4.

Assertion (A): The substring() method in Java returns a new string.


Reason (R): The String class in Java creates new objects for string manipulation.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q5.

Assertion (A): Strings are a primitive data type in Java.


Reason (R): Java provides wrapper classes for primitive data types.

Answer:

By : Souvik_Chakraborty_M.Sc_CS 6/17
 A is false, R is true.
(Explanation: Strings are not primitive; they are objects of the String
class.)

5. Methods

Q1.

Assertion (A): A method in Java can have multiple return statements.


Reason (R): The control of a method returns to the calling function after a return
statement.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q2.

Assertion (A): Methods can have the same name in Java.


Reason (R): Method overloading allows defining methods with the same name but
different parameter lists.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q3.

Assertion (A): A method declared as static can be called without creating an


object.
Reason (R): Static methods belong to the class, not an instance of the class.

Answer:

 A is true, R is true, and R is the correct explanation of A.

By : Souvik_Chakraborty_M.Sc_CS 7/17
Q4.

Assertion (A): A method can be recursive in Java.


Reason (R): Recursive methods call themselves to solve smaller sub-problems.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q5.

Assertion (A): Methods can only return void in Java.


Reason (R): The return type of a method defines what type of value it returns.

Answer:

 A is false, R is true.
(Explanation: Methods can return various data types, not just void.)

6. Constructors

Q1.

Assertion (A): Constructors do not have a return type.


Reason (R): Constructors are used for object initialization, not for returning
values.

Answer:

 A is true, R is true, and R is the correct explanation of A.

By : Souvik_Chakraborty_M.Sc_CS 8/17
Q2.

Assertion (A): If no constructor is defined in a class, Java provides a default


constructor.
Reason (R): The default constructor initializes object fields with default values.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q3.

Assertion (A): Constructors can be overloaded.


Reason (R): Constructor overloading allows creating multiple constructors with
different parameters.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q4.

Assertion (A): A constructor can be static.


Reason (R): Constructors are special methods called when an object is created,
and static methods can be accessed without an instance.

Answer:

 A is false, R is false.
(Explanation: Constructors cannot be static.)

Q5.

Assertion (A): Constructors cannot be inherited.


Reason (R): Constructors are not methods and are only called during the
instantiation of an object.
By : Souvik_Chakraborty_M.Sc_CS 9/17
Answer:

 A is true, R is true, and R is the correct explanation of A.

7. Method Overloading

Q1.

Assertion (A): Method overloading allows multiple methods with the same name
in a class.
Reason (R): The methods must differ in their parameter type, number, or order.

Answer:

 A is true, R is true, and R is the correct explanation of A.

Q2.

Assertion (A): The return type of overloaded methods must be different.


Reason (R): The Java compiler uses return type to differentiate overloaded
methods.

Answer:

 A is false, R is false.
(Explanation: Overloaded methods cannot be differentiated by return type
alone.)

Q3.

Assertion (A): Method overloading improves code readability.


Reason (R): Similar methods can be grouped under the same name with different
parameters.

Answer:

By : Souvik_Chakraborty_M.Sc_CS 10/17
 A is true, R is true, and R is the correct explanation of A.

Q4.

Assertion (A): Method overloading can only be done within the same class.
Reason (R): Method overriding is used to redefine methods in subclasses.

Answer:

 A is true, R is true, but R is not the correct explanation of A.


(Explanation: Method overloading can indeed only be done within the same
class.)

Q5.

Assertion (A): An overloaded method can have a static modifier.


Reason (R): Method overloading can be applied to both static and instance
methods.

Answer:

 A is true, R is true, and R is the correct explanation of A.

1. Arrays

Q1.
class ArrayExample {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int i = 1, j = 3, sum = 0;

sum += arr[i++] + arr[--j];


sum += arr[i]++ + arr[j--];
sum += arr[++i] + arr[j];
By : Souvik_Chakraborty_M.Sc_CS 11/17
[Link]("Sum: " + sum);
}
}

Output:
Sum: 145

Explanation:

1. arr[i++] + arr[--j]: i = 1, so arr[i++] = 20; --j = 2, so arr[2] = 30.


Sum becomes 20 + 30 = 50.
o Now i = 2, j = 2.
2. arr[i]++ + arr[j--]: arr[2]++ = 30 (post-increment) and arr[2] = 30.
Sum becomes 50 + 30 + 30 = 110.
o Now i = 2, j = 1.
3. arr[++i] + arr[j]: ++i = 3, so arr[3] = 40; arr[1] = 20. Sum becomes
110 + 40 + 20 = 145.

Q2.
class ArrayManipulation {
public static void main(String[] args) {
int[] a = {5, 10, 15, 20, 25};
int x = 0, y = 4, diff = 0;

diff = a[y--] - a[++x];


diff += a[x++] + a[--y];
diff -= a[y--] - a[--x];

[Link]("Difference: " + diff);


}
}

Output:
Difference: 15

Explanation:

1. a[y--] - a[++x]: a[4] = 25, ++x = 1, so a[1] = 10. Difference becomes


25 - 10 = 15.
o Now y = 3, x = 1.

By : Souvik_Chakraborty_M.Sc_CS 12/17
2. a[x++] + a[--y]: a[1] = 10 (post-increment), --y = 2, so a[2] = 15.
Difference becomes 15 + 10 + 15 = 40.
o Now x = 2, y = 2.
3. a[y--] - a[--x]: a[2] = 15, --x = 1, so a[1] = 10. Difference becomes
40 - (15 - 10) = 15.

Q3.
class ArrayRotation {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int n = [Link];

for (int i = 0; i < n; i++) {


int temp = arr[0];
for (int j = 0; j < n - 1; j++) {
arr[j] = arr[j + 1];
}
arr[n - 1] = temp;

// Printing the array after each rotation


for (int k = 0; k < n; k++) {
[Link](arr[k] + " ");
}
[Link]();
}
}
}

Output:
Copy code
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
1 2 3 4 5

Explanation: The program performs a left rotation on the array for n iterations.
Each iteration moves the first element to the end while shifting all others to the
left.

By : Souvik_Chakraborty_M.Sc_CS 13/17
Q4.
class ArraySum {
public static void main(String[] args) {
int[][] arr = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int rowSum = 0, colSum = 0;

for (int i = 0; i < [Link]; i++) {


rowSum += arr[i][i];
colSum += arr[i][[Link] - i - 1];
}

[Link]("Diagonal 1 Sum: " + rowSum);


[Link]("Diagonal 2 Sum: " + colSum);
}
}

Output:
Diagonal 1 Sum: 15
Diagonal 2 Sum: 15

Explanation:

 Diagonal 1: 1 + 5 + 9 = 15
 Diagonal 2: 3 + 5 + 7 = 15.

2. Strings

Q1.
class StringExample {
public static void main(String[] args) {
String s = "ICSE";
s = [Link](" 2024").toLowerCase();
s = [Link](0, 6);

StringBuilder sb = new StringBuilder(s);


[Link]();
s = [Link]();

[Link](s);
By : Souvik_Chakraborty_M.Sc_CS 14/17
}
}

Output:
420 202

Explanation:

1. [Link](" 2024") → ICSE 2024.


2. .toLowerCase() → icse 2024.
3. .substring(0, 6) → icse 2.
4. [Link]() → 420 202.

Q2.
class PalindromeCheck {
public static void main(String[] args) {
String str = "MADAM";
String rev = "";

for (int i = [Link]() - 1; i >= 0; i--) {


rev += [Link](i);
}

if ([Link](rev)) {
[Link]("Palindrome");
} else {
[Link]("Not a Palindrome");
}
}
}

Output:
Palindrome

Explanation: The input string MADAM matches its reverse, so it is a palindrome.

Q3.
class SubstringCount {
public static void main(String[] args) {
String s = "ICSE ICSE ICSE";

By : Souvik_Chakraborty_M.Sc_CS 15/17
String word = "ICSE";
int count = 0;

for (int i = 0; i <= [Link]() - [Link](); i++) {


if ([Link](i, i + [Link]()).equals(word)) {
count++;
}
}

[Link]("Count: " + count);


}
}

Output:
Count: 3

Explanation: The program iterates over the string and counts non-overlapping
occurrences of "ICSE".

Q4.
class ReplaceVowels {
public static void main(String[] args) {
String s = "ICSE 2024";
String result = "";

for (int i = 0; i < [Link](); i++) {


char c = [Link](i);
if ("AEIOUaeiou".indexOf(c) != -1) {
result += '*';
} else {
result += c;
}
}

[Link](result);
}
}

Output:
*CS* 2024

Explanation:
The vowels (I, E) are replaced with *.

By : Souvik_Chakraborty_M.Sc_CS 16/17
Q5.
class AnagramCheck {
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";

char[] arr1 = [Link]();


char[] arr2 = [Link]();

[Link](arr1);
[Link](arr2);

if ([Link](arr1, arr2)) {
[Link]("Anagrams");
} else {
[Link]("Not Anagrams");
}
}
}

Output:
Anagrams

Explanation:
Sorting the characters of both strings and comparing them shows that they are
anagrams.

XXXXX END XXXXX

Best Wish For your Pre-Board Examination

By : Souvik_Chakraborty_M.Sc_CS 17/17

Common questions

Powered by AI

Java does not support multiple inheritance directly through classes to avoid the complexity and ambiguity associated with it, such as the 'diamond problem.' Instead, Java achieves multiple inheritance through interfaces, allowing a class to implement multiple interfaces thereby inheriting their abstract methods .

Arrays in Java are of fixed size, meaning their length cannot be altered after they are created, unlike other collections like lists or sets which can dynamically resize. Arrays are not part of Java's collection framework but are a core language feature that provides fast index-based access to elements stored in contiguous memory locations, resulting in efficient retrieval and storage .

Method overloading allows multiple methods with the same name within a class, differing by parameter type, number, or order, thereby enhancing code readability and organization by logically grouping similar operations. Overloading cannot be distinguished solely by return type, making the parameter list the key determinant .

Strings in Java are immutable, meaning their value cannot be changed once created, enhancing security and performance by allowing the reuse of String literals through a shared string pool. The '==' operator compares reference equality, checking if two String references point to the same object, whereas 'equals()' checks content equality, comparing the actual string contents .

The main() method must be declared as 'public' because it serves as the entry point for the Java Virtual Machine (JVM) to start program execution. This visibility ensures that the JVM can access and execute the method regardless of where it is called from .

StringBuilder is preferred for string concatenation in loops because it is mutable, meaning it modifies existing objects rather than creating new ones every time a concatenation occurs. This approach reduces memory consumption and improves performance compared to using String, which is immutable and would create a new object for each concatenation within a loop .

Java is considered platform-independent because its source code is compiled into bytecode, which is a platform-neutral intermediate code understood by the Java Virtual Machine (JVM). This means that any system with a JVM can execute Java programs, regardless of the underlying hardware or operating system .

Garbage collection in Java manages memory automatically by reclaiming memory from objects no longer in use, thus preventing memory leaks. While developers can suggest the JVM run the garbage collector by invoking System.gc(), this does not guarantee its execution as the JVM ultimately decides when to perform garbage collection .

Constructors initialize new objects with default or specified values and do not have a return type. They cannot be static because they need to operate on specific instance data during object creation. Constructors also cannot be inherited as they are not normal methods but special class-specific functions used only during object instantiation .

In Java, 'while' loops are entry-controlled, meaning they check the condition before executing the loop body. 'For' loops are also entry-controlled and are typically used for iterating over arrays or collections, offering initialization, condition, and increment/decrement in a single line. 'Do-while' loops are exit-controlled, guaranteeing that the loop body executes at least once before the condition is checked .

You might also like