0% found this document useful (0 votes)
21 views11 pages

Untitled Document

The document provides answers and explanations for a series of Power Prep 2 questions related to Computer Applications, covering multiple-choice questions, coding outputs, naming questions, fill-in-the-blanks, and practical coding problems. It includes Java code examples for sorting names, reversing strings, extracting and sorting digits, and analyzing seat bookings in a cinema hall. Each section is detailed with explanations and expected outputs for clarity.

Uploaded by

astha8260
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)
21 views11 pages

Untitled Document

The document provides answers and explanations for a series of Power Prep 2 questions related to Computer Applications, covering multiple-choice questions, coding outputs, naming questions, fill-in-the-blanks, and practical coding problems. It includes Java code examples for sorting names, reversing strings, extracting and sorting digits, and analyzing seat bookings in a cinema hall. Each section is detailed with explanations and expected outputs for clarity.

Uploaded by

astha8260
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
You are on page 1/ 11

Here are the answers with explanations for your Power Prep 2 (Computer Applications)

questions:

---

Multiple-Choice Questions (MCQs) with Explanations

1. b) String replace(char oldChar, char newChar)


The replace() method is used to replace characters in a string.

2. a) trim()
The trim() method removes whitespace from the beginning and end of a string.

3. c) charAt()
The charAt(int index) method extracts a single character at the specified index.

4. b) Autoboxing
Automatic conversion of a primitive type (e.g., int) to its corresponding wrapper class
(Integer).

5. d) str.toUpperCase()
toUpperCase() is a method of the String class, not the Character wrapper class.

6. b) true
Character.isWhitespace('\n') returns true since \n is a whitespace character.

7. a) -1
indexOf('k') in "KOLKATA" returns -1 because the string has uppercase 'K', not lowercase 'k'.

8. c) wrapper class
Primitive types (e.g., int, char) have corresponding wrapper classes (Integer, Character).

9. a) Integer Wrapper Class


parseInt() converts a String to an int, and it's part of the Integer wrapper class.

10. a) Primitive type to String


valueOf() converts a primitive type (e.g., int) to a String.

11. b) false
endsWith("This") checks if the string ends with "This", which it does not.

12. c) +
The + operator concatenates strings in Java.

13. b) length()
The length() method returns the length of a string.
14. a) Today is Holiday
s.substring(0,7) extracts "Today i"; adding "Holiday" results in "Today is Holiday".

15. c) 150.0
Double.parseDouble("56.0") + Double.parseDouble("94.0") = 150.0.

16. d) 3760
"4.3756" → indexOf('.') = 1, extract "4" and "3756" → sum = 4 + 3756 = 3760.

17. b) -15
compareTo("State") compares "Status" and "State" lexicographically, resulting in -15.

18. d) char[] c = new char[4]


Correct way to declare an array of char in Java.

19. c) Both row and column


For multi-dimensional arrays, both dimensions must be defined.

20. b) 6
a[5] represents the 6th element since array indices start from 0.

21. a) 1416

x[2] = 14, x[4] = 16 → "14" + "16" = "1416" (String concatenation).

22. c) 40 bytes
In Unicode, each char takes 4 bytes.

23. a) 12. a[2+1] → a[3] → value = 12.

24. a) Both Assertion(A) and Reason(R) are true, and Reason(R) is a correct explanation
Arrays store multiple values of the same type.

25. c) subscripted variable


Array elements are accessed using subscripts (indices).

26. b) 59
If 59 locations are reserved, all can be used.

27. a) Both Assertion(A) and Reason(R) are true, and Reason(R) is a correct explanation
arr.length returns the number of elements.

28. c) Arrays
The image likely represents arrays.

29. d) Assertion(A) is false and Reason(R) is true


Array indexing starts from 0, so for(int i=1; i<=n; i++) is incorrect.
30. d) Arrays is a heterogeneous data structure.
Arrays in Java store only homogeneous data (same type).

31. b) interff
substring(10,15) extracts "inter", substring(25,25+x) extracts "ff".

32. a) 11511
"5" is concatenated, not added: 1 + 10 + "5" + 1 + 10 = "11511".

33. a) equals()
equals() compares two strings for equality.

34. a) true
equals() checks content equality, so "Java".equals(new String("Java")) returns true.

35. b) -26
compareToIgnoreCase("canva") returns the difference between 'J' and 'c'.

36. d) int arr [][]={{1,2},{3}};


Correct way to initialize a multi-dimensional array.

37. b) Sequential
Array elements are stored sequentially in memory.

38. a) It copies what is in cell 0 into all other cells.


num[x] = num[0] assigns num[0] to all elements.

39. c) Compilation Error


int arr[2]; is incorrect in Java.

40. c) 10
Sum of all elements is 45, sum/5 = 10.

---

Section B: Coding & Output Questions

1. Output of Given Snippet

int a[]={1,3,5,7,11}; int k=2; int i=--a[2]; // a[2] (5) decrements to 4 int j=a[++k]; // k=3, a[3] = 7

System.out.println("The value of i is:"+i); System.out.println("The value of j is:"+j);

Output:
The value of i is: 4
The value of j is: 7

2. Output of 2D String Array

String str[][] = {{"I","am","doing","best"},{"I","will","achieve","100","in","Comp","App"}};


System.out.println(str.length); // 2
System.out.println(str[0][1].compareToIgnoreCase(str[1][1])); // "am" vs "will" = -8
System.out.println(str[0][2].length()); // "doing" = 5 System.out.println(str[1][2].indexOf('e') +
str[1][3].charAt(0)); // "achieve" indexOf('e') = 3, '1' ASCII = 49, sum = 52

Output:

2
-8
5
52

---

3. Naming Questions

a) Binary Search (Works on sorted arrays)

b) Selection Sort (Moves the smallest element to the first position)

---

4. Fill in the Blanks

for(int i=0; i<sales.length; i++){ // Blank1: sales.length int total = 0; for(int j=0;
j<sales[i].length; j++){ total += sales[i][j]; // Blank2: total += sales[i][j]; }
System.out.println("Total sales for region " + (i+1) + ": " + total); }

---

5. Trim & Convert to Lowercase


String cleanedInput = input.trim().toLowerCase(); // Blank1 int length = cleanedInput.length();
// Blank2

Would you like complete Java programs for Section B practical questions? Here are the
complete Java programs for the Section B practical questions:

---

1. Separate Boys & Girls Names and Sort Girls' Names

Problem:

Accept 10 student names.

Store girls' and boys' names in separate arrays.

Sort the girls' names using Selection Sort and display them.

Solution:

import java.util.Arrays; import java.util.Scanner;

public class StudentSort { public static void main(String[] args) { Scanner sc = new
Scanner(System.in); String students[] = new String[10]; String girls[] = new String[5]; String
boys[] = new String[5];
int gIndex = 0, bIndex = 0;

System.out.println("Enter 10 student names:");


for (int i = 0; i < 10; i++) {
students[i] = sc.next();
// Assuming names ending with 'a' are girls (for simplicity)
if (students[i].endsWith("a")) {
girls[gIndex++] = students[i];
} else {
boys[bIndex++] = students[i];
}
}

// Sorting girls' names using Selection Sort


for (int i = 0; i < gIndex - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < gIndex; j++) {
if (girls[j].compareTo(girls[minIndex]) < 0) {
minIndex = j;
}
}
String temp = girls[minIndex];
girls[minIndex] = girls[i];
girls[i] = temp;
}

System.out.println("Sorted Girls' Names:");


for (int i = 0; i < gIndex; i++) {
System.out.println(girls[i]);
}
sc.close();
}

---

2. Reverse a String

Problem:

Accept a string and print the reverse of the entire string.

Solution:

import java.util.Scanner;

public class ReverseString { public static void main(String[] args) { Scanner sc = new
Scanner(System.in); System.out.println("Enter a string:"); String str = sc.nextLine();

String reversed = new StringBuilder(str).reverse().toString();


System.out.println("Reversed String: " + reversed);

sc.close();
}

Example Input: color box Output: xob roloc

---
3. Reverse Each Word in a String

Problem:

Accept a string and reverse each word while keeping the order of words intact.

Solution:

import java.util.Scanner;

public class ReverseWords { public static void main(String[] args) { Scanner sc = new
Scanner(System.in); System.out.println("Enter a sentence:"); String sentence =
sc.nextLine();

String words[] = sentence.split(" ");


for (int i = 0; i < words.length; i++) {
words[i] = new StringBuilder(words[i]).reverse().toString();
}

System.out.println("Reversed Words: " + String.join(" ", words));


sc.close();
}

Example Input: color box Output: roloc xob

---

4. Extract Digits and Sort Them

Problem:

Accept a number, extract digits, store in an array, and sort using Selection Sort.

Solution:

import java.util.Scanner;

public class SortDigits { public static void main(String[] args) { Scanner sc = new
Scanner(System.in); System.out.println("Enter a number:"); int n = sc.nextInt();

String numStr = Integer.toString(n);


int digits[] = new int[numStr.length()];

// Store digits in the array


for (int i = 0; i < numStr.length(); i++) {
digits[i] = Character.getNumericValue(numStr.charAt(i));
}

// Selection Sort
for (int i = 0; i < digits.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < digits.length; j++) {
if (digits[j] < digits[minIndex]) {
minIndex = j;
}
}
int temp = digits[minIndex];
digits[minIndex] = digits[i];
digits[i] = temp;
}

System.out.println("Sorted Digits:");
for (int digit : digits) {
System.out.println(digit);
}

sc.close();
}

Example Input: 549 Output:

459

---

5. Find the First Vowel in the Longest Word and Its Count

Problem:

Accept a sentence, find the longest word, and count occurrences of the first vowel.

Solution:

import java.util.Scanner;
public class LongestWordVowel { public static void main(String[] args) { Scanner sc = new
Scanner(System.in); System.out.println("Enter a sentence:"); String sentence =
sc.nextLine();

String words[] = sentence.split(" ");


String longestWord = "";
for (String word : words) {
if (word.length() > longestWord.length()) {
longestWord = word;
}
}

// Find first vowel


char firstVowel = ' ';
int count = 0;
for (char c : longestWord.toCharArray()) {
if ("aeiouAEIOU".indexOf(c) != -1) {
firstVowel = c;
break;
}
}

// Count occurrences
for (char c : longestWord.toCharArray()) {
if (c == firstVowel) {
count++;
}
}

System.out.println("Longest Word: " + longestWord);


System.out.println("First Vowel: " + firstVowel);
System.out.println("Occurrences: " + count);
sc.close();
}

Example Input: The method is overloaded for different primitive types. Output:

Longest Word: overloaded


First Vowel: o
Occurrences: 2

---

6. Cinema Hall Seat Booking System


Problem:

Use a 5x5 matrix to represent seat bookings.

1 means booked, 0 means available.

Calculate booked seats, available seats, row/column sums, and diagonal sums.

Solution:

import java.util.Scanner;

public class CinemaHall { public static void main(String[] args) { Scanner sc = new
Scanner(System.in); int seats[][] = new int[5][5];

System.out.println("Enter 5x5 seating arrangement (1 for booked, 0 for available):");


for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
seats[i][j] = sc.nextInt();
}
}

int booked = 0, available = 0;


int rowSum[] = new int[5], colSum[] = new int[5];
int leftDiagonal = 0, rightDiagonal = 0;

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


for (int j = 0; j < 5; j++) {
if (seats[i][j] == 1) booked++;
else available++;

rowSum[i] += seats[i][j];
colSum[j] += seats[i][j];

if (i == j) leftDiagonal += seats[i][j];
if (i + j == 4) rightDiagonal += seats[i][j];
}
}

System.out.println("Total Booked Seats: " + booked);


System.out.println("Total Available Seats: " + available);
System.out.println("Left Diagonal Sum: " + leftDiagonal);
System.out.println("Right Diagonal Sum: " + rightDiagonal);

sc.close();
}

🚀
This program calculates all required values efficiently. Let me know if you need
modifications!

Give this all in pdf format together

You might also like