Solve the following.
Show answer
Answer
Final answer: 5952
48 past-paper questions on Arrays, Strings from ISC Class 12 Computer Science papers (2026-2017), newest first, in full. Questions 1-20 are on this page, 20 to a page. Tap "Show answer" under a question to see its answer. The syllabus covers Single and multi-dimensional arrays, Address calculations, Searching and sorting techniques, String class and operations and StringBuffer class.
Nothing matches. Try fewer letters.
Solve the following.
Answer
Final answer: 5952
Write the program described below.
Answer
import java.util.Scanner;
class PendulumS
{
String wrd;
String newwrd;
PendulumS(String k)
{
wrd = k;
newwrd = "";
}
int minCharIndex(String str)
{
int minIdx = 0;
char minCh = str.charAt(0);
for (int i = 1; i < str.length(); i++)
{
if (str.charAt(i) < minCh)
{
minCh = str.charAt(i);
minIdx = i;
}
}
return minIdx;
}
void arrange()
{
String temp = wrd;
int len = wrd.length();
for (int i = 0; i < len; i++)
{
int idx = minCharIndex(temp);
char ch = temp.charAt(idx);
temp = temp.substring(0, idx) + temp.substring(idx + 1);
if (i == 0)
{
newwrd = "" + ch;
}
else if (i % 2 == 1)
{
newwrd = newwrd + ch; // place to the right
}
else
{
newwrd = ch + newwrd; // place to the left
}
}
}
void display()
{
System.out.println("Original word: " + wrd);
System.out.println("Rearranged word: " + newwrd);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word in uppercase: ");
String input = sc.next();
PendulumS obj = new PendulumS(input);
obj.arrange();
obj.display();
}
}Explanation: minCharIndex finds the index of the character with the minimum ASCII value in a string. arrange extracts characters in ascending ASCII order and alternately appends them to the right and prepends them to the left of the center character, forming the pendulum arrangement.Answer the following.
Answer
Final answer: 5020
Solve the following.
Answer
Final answer: 1266
Choose the correct option.
StringBuffer x = new StringBuffer("Hello");
StringBuffer y = new StringBuffer("HELLO");
System.out.println(x.equalsIgnoreCase(y));Answer
Correct option: c
Choose the correct option.
int x = 234;
String str = String.valueOf(x) ;
System.out.println(str.length( ));
System.out.println( x + x );
System.out.println( str + str );Answer
Correct option: d
Choose the correct option.
int [ ] [ ] x = { {8,9} , {5,6,7},{1,2,3,4}}, what are x[0].length, x[1].length, and x[2].length?Answer
Correct option: b
Answer the following question.
Answer
Study the Assertion and Reason and choose the correct option.
Assertion: In Java, the String class is used to create and manipulate strings, and it is immutable.
Reason: Immutability ensures that once a String object is created, its value cannot be changed.
Answer
Official answer keyCorrect option: (a)
Solve the following.
Answer
Official answer keyFinal answer: 1337
What is the output of the following code?
void check1( )
{
String x[ ] = {"Kolkata", "Chennai", "Delhi"};
String y[ ] = {"Kolkata", "Chennai", "Delhi"};
System.out.println(x.equals(y));
}Answer
Choose the correct option.
public class Test
{
public static void main( )
{
char [ ][ ] matrix = {{'a','b','c'},{'p','q','r'},{'x','y','z'}};
for( int i=0 ; i<3 ; i++)
System.out.print( matrix[i] [1] + " " );
}
}Answer
Correct option: d
Choose the correct option.
Answer
Correct option: a
Answer the following question.
String s1 = "Gujarat";
String s2 = new String("Gujarat");
String s3 = new String("Gujarat");
String s4 = "Gujarat";How many String objects are created in the above snippet? Justify your answer.Answer
Write the program described below.
Answer
Official answer keyimport java.util.Scanner;
class Mixarray
{
int arr[];
int cap;
Mixarray(int mm)
{
cap = mm;
arr = new int[cap];
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter " + cap + " elements:");
for (int i = 0; i < cap; i++)
arr[i] = sc.nextInt();
}
Mixarray mix(Mixarray P, Mixarray Q)
{
Mixarray res = new Mixarray(6);
int k = 0;
for (int i = 0; i < 3; i++)
res.arr[k++] = P.arr[i];
for (int i = 0; i < 3; i++)
res.arr[k++] = Q.arr[i];
return res;
}
void display()
{
System.out.println("Resultant array:");
for (int i = 0; i < cap; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter capacity of first array (>=3): ");
int c1 = sc.nextInt();
Mixarray P = new Mixarray(c1);
P.input();
System.out.print("Enter capacity of second array (>=3): ");
int c2 = sc.nextInt();
Mixarray Q = new Mixarray(c2);
Q.input();
Mixarray R = new Mixarray(6);
Mixarray res = R.mix(P, Q);
res.display();
}
}Explanation: mix(P,Q) creates a new Mixarray object of capacity 6, copies the first three elements of P into it followed by the first three elements of Q, and returns it. Tested with Array1={78,90,100,45,67} and Array2={10,67,200,90}: the program correctly outputs the resultant array 78 90 100 10 67 200 (verified by running the code), matching the example in the question.Write the program described below.
Sample input/output
Sample input
Enter first word->earth
Enter second word->heart
Output
earth and heart are anagram of each otherAnswer
import java.util.Scanner;
class Anagram
{
String s1, s2; // the two words in lower case
int l1, l2; // number of characters in s1 and s2
// parameterised constructor
Anagram(String s1, String s2)
{
this.s1 = s1;
this.s2 = s2;
l1 = s1.length();
l2 = s2.length();
}
// sorts the characters of c[] in ascending order using bubble sort
void sort(char c[])
{
int n = c.length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1 - i; j++)
if (c[j] > c[j + 1])
{
char t = c[j];
c[j] = c[j + 1];
c[j + 1] = t;
}
}
// checks if str1 and str2 are anagrams by sorting both and comparing
boolean areAnagram(char[] str1, char[] str2)
{
if (str1.length != str2.length)
return false;
sort(str1);
sort(str2);
for (int i = 0; i < str1.length; i++)
if (str1[i] != str2[i])
return false;
return true;
}
// converts s1 and s2 to character arrays and displays the result
void check()
{
char c1[] = s1.toCharArray();
char c2[] = s2.toCharArray();
if (areAnagram(c1, c2))
System.out.println(s1 + " and " + s2 + " are anagram of each other");
else
System.out.println(s1 + " and " + s2 + " are not anagram of each other");
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first word->");
String s1 = sc.next();
System.out.print("Enter second word->");
String s2 = sc.next();
Anagram ob = new Anagram(s1, s2);
ob.check();
}
}Explanation: sort() rearranges a character array into ascending order with bubble sort; areAnagram() sorts copies of both words and returns true only if every position then matches (same length and same multiset of letters). check() converts the two stored words to char arrays and prints the appropriate message. Tested (run for real): "earth"/"heart" printed "earth and heart are anagram of each other" (matching the sample), "silent"/"listen" also correctly reported as anagrams, and "hello"/"world" was correctly reported as not anagrams.What is the output of the following code?
System.out.println("RocKet".substring(0, 4).compareTo("Rock".replace('m', 'o')));Answer
Answer the following question.
int arr[ ], if its first index is $x$ and the last index is $y$ ?Answer
Choose the correct option.
| String | Description |
|---|---|
| StringA | Password entered by the user |
| StringB | Password stored by the system |
Answer
Correct option: b
Solve the following.
Answer
Final answer: 2310
Arrays, Strings is a unit of the ISC Class 12 Computer Science syllabus.
The syllabus lists these topics: Single and multi-dimensional arrays, Address calculations, Searching and sorting techniques, String class and operations and StringBuffer class.
Prashnika has 48 past-paper questions on Arrays, Strings, from papers of 2017-2026, asked in 10 different years. By kind: Numerical (13), Program (13), MCQ (12) and Short answer (4). They carry between 1 and 15 marks, and most are worth 2 marks.
To practise, use the 2D array visualiser and the paper generator, which can build a practice paper from this chapter.
The syllabus lists Single and multi-dimensional arrays, Address calculations, Searching and sorting techniques, String class and operations and StringBuffer class.
Prashnika has 48 questions on Arrays, Strings, from papers of 2017-2026 (board, specimen, improvement and practice papers together).
By kind: Numerical (13), Program (13), MCQ (12) and Short answer (4).