Computer Applications
Consider the following program segment in which the statements are jumbled, choose the correct order of statements to check if a given word is Palindrome or not.
boolean palin(String w) {
boolean isPalin;
w = w.toUpperCase();
int l = w.length();
isPalin = false; // Stmt (1)
for (int i = 0; i < l / 2; i++) {
char c1 = w.charAt(i),
c2 = w.charAt(l - 1 - i); // Stmt (2)
if (c1 != c2) {
break; // Stmt (3)
isPalin = true; // Stmt (4)
}
}
return isPalin;
}
Answer
(4) (2) (1) (3)
Reason — To understand why the correct order is (4) (2) (1) (3), let’s analyze the program's purpose and the logic step by step. The goal of the program is to check if a given word is a Palindrome. A palindrome is a word that reads the same forward and backward, like "RADAR" or "LEVEL".
Steps in the Correct Order:
1. Statement (4): isPalin = true;
- We assume the word is a palindrome by setting
isPalin
totrue
at the start. This helps simplify our logic because we only need to find a mismatch to prove otherwise.
2. Statement (2): char c1 = w.charAt(i), c2 = w.charAt(l - 1 - i);
- In the loop, we pick one character from the start (
c1
) and the corresponding character from the end (c2
) of the word to compare them.
3. Statement (1): if (c1 != c2)
- If the characters do not match, the word is not a palindrome. To indicate this, we set
isPalin = false
.
4. Statement (3): break;
- If we find even one mismatch, there’s no need to check further. We use
break
to exit the loop immediately.
Program segment with statements in correct order:
boolean palin(String w) {
boolean isPalin;
w = w.toUpperCase();
int l = w.length();
isPalin = true; // Stmt (4)
for (int i = 0; i < l / 2; i++) {
char c1 = w.charAt(i),
c2 = w.charAt(l - 1 - i); // Stmt (2)
if (c1 != c2) {
isPalin = false; // Stmt (1)
break; // Stmt (3)
}
}
return isPalin;
}
Related Questions
Which of the following returns a value greater than or equal to 0?
The following code to compare two strings is compiled, the following syntax error was displayed – incompatible types – int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate() { String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); System.out.println(x); }
The output of the statement "talent".compareTo("genius") is:
- 11
- –11
- 0
- 13
A university student's registration number follows the format:
<CourseCode><Year><CollegeCode><RollNumber>
where
Component Description CourseCode A 3-letter code representing the course (e.g., CSE for Computer Science, ECE for Electronics & Communication) Year The last two digits of the admission year. CollegeCode A 3-digit code representing the college RollNumber A 4-digit unique student roll number. Examples
Registration Number Course Code Admission Year College Code Roll Number CSE240011023 CSE 24 001 1023 ECE252104297 ECE 25 210 4297 ASE230277259 ASE 23 027 7259 Define a class that accepts a student's registration number as input, extracts the relevant details, and displays them in the specified format.
import java.util.Scanner; public class KboatStuRegNum { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter reg. no.: "); _______(1)_________ int l = n.length(); if (l != 12) { System.out.println("Invalid reg. no."); System.exit(0); } _______(2)_________ _______(3)_________ _______(4)_________ _______(5)_________ System.out.println("Course Code : " + cc); System.out.println("Admission Year : " + yr); System.out.println("College Code : " + cl); System.out.println("Roll Number : " + rNo); } }