Untitled Document
Untitled Document
questions:
---
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).
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.
20. b) 6
a[5] represents the 6th element since array indices start from 0.
21. a) 1416
22. c) 40 bytes
In Unicode, each char takes 4 bytes.
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.
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.
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'.
37. b) Sequential
Array elements are stored sequentially in memory.
40. c) 10
Sum of all elements is 45, sum/5 = 10.
---
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
Output:
The value of i is: 4
The value of j is: 7
Output:
2
-8
5
52
---
3. Naming Questions
---
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); }
---
Would you like complete Java programs for Section B practical questions? Here are the
complete Java programs for the Section B practical questions:
---
Problem:
Sort the girls' names using Selection Sort and display them.
Solution:
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;
---
2. Reverse a String
Problem:
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();
sc.close();
}
---
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();
---
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();
// 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();
}
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();
// Count occurrences
for (char c : longestWord.toCharArray()) {
if (c == firstVowel) {
count++;
}
}
Example Input: The method is overloaded for different primitive types. Output:
---
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];
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];
}
}
sc.close();
}
🚀
This program calculates all required values efficiently. Let me know if you need
modifications!