VNSGU BCA Sem 4: Java Programming (403) Practical Solutions - April 2025 Set BΒΆ
Paper Details
- Subject: Java Programming Language
- Subject Code: 403
- Set: B
- Semester: 4
- Month/Year: April 2025
- Max Marks: 25
- Time Recommendation: 45 Minutes
- Paper: View Paper | Download PDF
Questions & SolutionsΒΆ
All questions are compulsoryΒΆ
Q1: String OperationsΒΆ
Max Marks: 20
Write a Java program to input a string from the user and perform the following operations:
- Convert it to uppercase
- Find the length of the string
- Reverse the string
- Count the number of vowels in the string
1. Convert to UppercaseΒΆ
Transform all characters in the string to uppercase using built-in String methods.
Hint
Use the toUpperCase() method from the String class. This method returns a new string with all characters converted to uppercase.
flowchart TD
A["Input String"] --> B["str.toUpperCase()"]
B --> C["UPPERCASE STRING"]
View Solution & Output
// Convert string to uppercase
String original = "Hello World";
String uppercase = original.toUpperCase();
System.out.println("Original: " + original);
System.out.println("Uppercase: " + uppercase);
Output:
Step-by-Step Explanation:
- Initialization: Create a string variable with the input text
- Logic Flow: Call
toUpperCase()method on the string object. This method iterates through each character and converts lowercase letters (a-z) to uppercase (A-Z) - Completion: Store the result in a new string variable (strings are immutable in Java)
2. Find Length of StringΒΆ
Calculate the total number of characters in the string.
Hint
Use the length() method. It returns the count of characters including spaces and special characters.
flowchart TD
A["Input String"] --> B["str.length()"]
B --> C["Length = 11"]
View Solution & Output
// Find length of string
String text = "Hello World";
int length = text.length();
System.out.println("String: " + text);
System.out.println("Length: " + length);
Output:
Step-by-Step Explanation:
- Initialization: Create a string with the input text
- Logic Flow: Call
length()method which returns an integer count of all characters including spaces - Completion: Store and display the length
3. Reverse the StringΒΆ
Create a new string with characters in reverse order.
Hint
Use a loop to iterate from the last character to the first. Use charAt(index) to access individual characters.
flowchart TD
A["Input String"] --> B["i = length-1"]
B --> C{"i >= 0?"}
C -->|Yes| D["Append charAt(i)"]
D --> E["i--"]
E --> C
C -->|No| F["Reversed String"]
View Solution & Output
// Reverse the string
String original = "Hello";
String reversed = "";
// Loop from last index to first
for (int i = original.length() - 1; i >= 0; i--) {
reversed += original.charAt(i);
}
System.out.println("Original: " + original);
System.out.println("Reversed: " + reversed);
Output:
Step-by-Step Explanation:
- Initialization: Initialize an empty string to store the reversed result
- Logic Flow: Loop from the last index (
length - 1) down to 0. UsecharAt(i)to get each character and append it to the result string - Completion: The result string now contains characters in reverse order
4. Count Vowels in StringΒΆ
Count the number of vowels (a, e, i, o, u) in the string.
Hint
Convert the string to lowercase first for easier comparison. Loop through each character and check if it's a vowel.
flowchart TD
A["Input String"] --> B["Convert to lowercase"]
B --> C["Loop through each char"]
C --> D{"Is vowel?"}
D -->|Yes| E["Increment count"]
D -->|No| F["Continue"]
E --> G["Next char"]
F --> G
G --> H{"More chars?"}
H -->|Yes| C
H -->|No| I["Display count"]
View Solution & Output
// Count vowels in string
String text = "Hello World";
int vowelCount = 0;
// Convert to lowercase for uniform checking
String lower = text.toLowerCase();
// Loop through each character
for (int i = 0; i < lower.length(); i++) {
char ch = lower.charAt(i);
// Check if character is a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowelCount++;
}
}
System.out.println("String: " + text);
System.out.println("Vowel Count: " + vowelCount);
Output:
Step-by-Step Explanation:
- Initialization: Initialize vowel counter to 0
- Logic Flow: Convert string to lowercase, then loop through each character. Check if it's one of the vowels (a, e, i, o, u)
- Completion: Increment counter for each vowel found and display the result
5. Complete Main ProgramΒΆ
Combine all operations with user input.
View Solution & Output
import java.util.Scanner;
public class StringOperations {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input string from user
System.out.print("Enter a string: ");
String input = sc.nextLine();
// 1. Convert to uppercase
String uppercase = input.toUpperCase();
System.out.println("\n1. Uppercase: " + uppercase);
// 2. Find length
int length = input.length();
System.out.println("2. Length: " + length);
// 3. Reverse the string
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
System.out.println("3. Reversed: " + reversed);
// 4. Count vowels
int vowelCount = 0;
String lower = input.toLowerCase();
for (int i = 0; i < lower.length(); i++) {
char ch = lower.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowelCount++;
}
}
System.out.println("4. Vowel Count: " + vowelCount);
sc.close();
}
}
Output:
Enter a string: Programming
1. Uppercase: PROGRAMMING
2. Length: 11
3. Reversed: gnimmargorP
4. Vowel Count: 3
Step-by-Step Explanation:
- Initialization: Create Scanner object for input, prompt user for string
- Logic Flow:
- Convert to uppercase using
toUpperCase() - Get length using
length() - Reverse using loop with
charAt() - Count vowels by checking each character
- Convert to uppercase using
- Completion: Display all results and close scanner
Concept Deep Dive: String Immutability
Important: Strings in Java are immutable. Methods like toUpperCase() don't modify the original string; they return a new string object.
String s = "hello";
s.toUpperCase(); // Returns "HELLO" but s remains "hello"
s = s.toUpperCase(); // Now s is "HELLO"
For frequent modifications, consider using StringBuilder which is mutable and more efficient.
Q2: Viva PreparationΒΆ
Max Marks: 5
Potential Viva Questions
- Q: What is the difference between String and StringBuilder?
-
A: String is immutable (cannot be changed after creation), while StringBuilder is mutable. StringBuilder is more efficient for frequent modifications.
-
Q: Why use
toLowerCase()before counting vowels? -
A: To handle both uppercase and lowercase vowels uniformly. Without it, 'A' and 'a' would need separate checks.
-
Q: What does
charAt()method do? -
A: Returns the character at the specified index. Index starts from 0.
-
Q: How does the
length()method work? -
A: Returns the total number of characters in the string, including spaces and special characters.
-
Q: Can we reverse a string without using a loop?
-
A: Yes, using StringBuilder's
reverse()method:new StringBuilder(str).reverse().toString() -
Q: What happens if we try to access an index beyond the string length?
-
A: It throws a
StringIndexOutOfBoundsException. -
Q: How would you count consonants instead of vowels?
- A: Count all alphabetic characters that are not vowels. Check using
Character.isLetter(ch)and exclude vowels.
Common Pitfalls
- Index out of bounds: Remember index starts at 0, so last index is
length() - 1 - Case sensitivity: Always consider case when comparing characters
- Empty strings: Check for empty input to avoid unexpected behavior
- String concatenation in loops: In loops, StringBuilder is more efficient than repeated string concatenation
- Scanner nextLine(): After using
nextInt()or other next methods, consume the newline beforenextLine()
Related HTML TutorialsΒΆ
Working with text in Java? You might also be interested in:
- HTML Text Formatting - Learn about text formatting in HTML
- HTML Lists Tutorial - Learn about organizing content with lists
Quick NavigationΒΆ
Related SolutionsΒΆ
| Set | Link |
|---|---|
| Set A | Solutions |
| Set B | Current Page |
| Set C | Solutions |
| Set D | Solutions |
| Set E | Solutions |
| Set F | Solutions |
Last Updated: April 2026