Skip to main content

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.

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:

Original: Hello World
Uppercase: HELLO WORLD

Step-by-Step Explanation:

  1. Initialization: Create a string variable with the input text
  2. 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)
  3. 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.

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:

String: Hello World
Length: 11

Step-by-Step Explanation:

  1. Initialization: Create a string with the input text
  2. Logic Flow: Call length() method which returns an integer count of all characters including spaces
  3. 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.

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:

Original: Hello
Reversed: olleH

Step-by-Step Explanation:

  1. Initialization: Initialize an empty string to store the reversed result
  2. Logic Flow: Loop from the last index (length - 1) down to 0. Use charAt(i) to get each character and append it to the result string
  3. 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.

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:

String: Hello World
Vowel Count: 3

Step-by-Step Explanation:

  1. Initialization: Initialize vowel counter to 0
  2. Logic Flow: Convert string to lowercase, then loop through each character. Check if it's one of the vowels (a, e, i, o, u)
  3. 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:

  1. Initialization: Create Scanner object for input, prompt user for string
  2. Logic Flow:
    • Convert to uppercase using toUpperCase()
    • Get length using length()
    • Reverse using loop with charAt()
    • Count vowels by checking each character
  3. 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
  1. 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.
  2. 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.
  3. Q: What does charAt() method do?

    • A: Returns the character at the specified index. Index starts from 0.
  4. Q: How does the length() method work?

    • A: Returns the total number of characters in the string, including spaces and special characters.
  5. Q: Can we reverse a string without using a loop?

    • A: Yes, using StringBuilder's reverse() method: new StringBuilder(str).reverse().toString()
  6. Q: What happens if we try to access an index beyond the string length?

    • A: It throws a StringIndexOutOfBoundsException.
  7. 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 before nextLine()

Working with text in Java? You might also be interested in:


Quick Navigation

SetLink
Set ASolutions
Set BCurrent Page
Set CSolutions
Set DSolutions
Set ESolutions
Set FSolutions

Last Updated: April 2026