Skip to main content
Unlisted page
This page is unlisted. Search engines will not index it, and only users having a direct link can access it.

VNSGU BCA Sem 4: Java Programming (403) Practical Solutions - April 2025 Set D

Paper Details
  • Subject: Java Programming Language
  • Subject Code: 403
  • Set: D
  • 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: Command Line Arguments - Sorting​

Max Marks: 20

Write a Java program that takes five numbers from the command line and prints them in ascending order.

1. Understanding Command Line Arguments​

Learn how to pass and process arguments from the command line.

Hint
  • Command line arguments are passed as String array args to main method
  • Use args.length to check number of arguments
  • Convert strings to numbers using Integer.parseInt() or Double.parseDouble()
  • Use Arrays.sort() or implement sorting algorithm

2. Convert and Store Arguments​

Parse command line arguments and store in an array.

Hint

Loop through args array, convert each string to integer using Integer.parseInt().

View Solution - Step 1
// Convert command line arguments to integers
int[] numbers = new int[5];

for (int i = 0; i < 5; i++) {
numbers[i] = Integer.parseInt(args[i]);
}

Step-by-Step Explanation:

  1. Initialization: Create int array of size 5
  2. Logic Flow: Loop through args array, parse each element to int
  3. Completion: Array now contains 5 integers

3. Sort the Array​

Sort the numbers in ascending order.

Hint

Use Arrays.sort() method from java.util package for easy sorting, or implement bubble sort manually.

View Solution - Step 2
import java.util.Arrays;

// Sort the array using built-in method
Arrays.sort(numbers);

// OR implement Bubble Sort manually:
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = 0; j < numbers.length - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
// Swap elements
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}

Step-by-Step Explanation:

  1. Initialization: Array with unsorted numbers
  2. Logic Flow:
    • Arrays.sort(): Built-in quicksort algorithm, O(n log n)
    • Bubble Sort: Compare adjacent elements, swap if out of order, O(n²)
  3. Completion: Array is sorted in ascending order

4. Complete Main Program​

Full solution with error handling.

View Complete Solution & Output
import java.util.Arrays;

public class CommandLineSort {
public static void main(String[] args) {
// Check if exactly 5 arguments are provided
if (args.length != 5) {
System.out.println("Error: Please provide exactly 5 numbers.");
System.out.println("Usage: java CommandLineSort <num1> <num2> <num3> <num4> <num5>");
return;
}

try {
// Create array to store numbers
int[] numbers = new int[5];

// Convert command line arguments to integers
System.out.println("Original numbers:");
for (int i = 0; i < 5; i++) {
numbers[i] = Integer.parseInt(args[i]);
System.out.print(numbers[i] + " ");
}
System.out.println();

// Sort the array
Arrays.sort(numbers);

// Print sorted numbers
System.out.println("Numbers in ascending order:");
for (int num : numbers) {
System.out.print(num + " ");
}
System.out.println();

} catch (NumberFormatException e) {
System.out.println("Error: All arguments must be valid integers.");
System.out.println("Example: java CommandLineSort 5 2 8 1 9");
}
}
}

Compile and Run:

javac CommandLineSort.java
java CommandLineSort 5 2 8 1 9

Output:

Original numbers:
5 2 8 1 9
Numbers in ascending order:
1 2 5 8 9

Step-by-Step Explanation:

  1. Initialization: Check args.length to ensure 5 arguments provided
  2. Logic Flow:
    • Parse each argument to integer using Integer.parseInt()
    • Use Arrays.sort() for efficient sorting
    • Display original and sorted arrays
  3. Completion: Program displays numbers in ascending order

5. Alternative: Manual Sorting without Arrays.sort()​

Implement sorting manually for learning purposes.

Manual Sorting Solution
public class ManualSort {
public static void main(String[] args) {
if (args.length != 5) {
System.out.println("Please provide exactly 5 numbers.");
return;
}

try {
// Parse arguments
int[] numbers = new int[5];
for (int i = 0; i < 5; i++) {
numbers[i] = Integer.parseInt(args[i]);
}

// Selection Sort algorithm
for (int i = 0; i < numbers.length - 1; i++) {
int minIndex = i;

// Find minimum element in remaining array
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[j] < numbers[minIndex]) {
minIndex = j;
}
}

// Swap found minimum with first element
int temp = numbers[minIndex];
numbers[minIndex] = numbers[i];
numbers[i] = temp;
}

// Print result
System.out.println("Sorted numbers:");
for (int num : numbers) {
System.out.print(num + " ");
}
System.out.println();

} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter integers only.");
}
}
}

Step-by-Step Explanation:

  1. Initialization: Parse command line arguments to integer array
  2. Logic Flow:
    • Selection Sort: Divide array into sorted and unsorted parts
    • Find minimum in unsorted part, swap with first unsorted element
    • Repeat until entire array is sorted
  3. Completion: Array sorted without using built-in methods
Concept Deep Dive: Sorting Algorithms

Common Sorting Algorithms:

  • Arrays.sort(): Uses Dual-Pivot Quicksort, O(n log n) average
  • Bubble Sort: Simple but slow, O(n²), good for learning
  • Selection Sort: O(n²), minimizes swaps
  • Insertion Sort: O(n²), efficient for small or nearly sorted data

Command Line Arguments:

  • Always check args.length before accessing
  • Parse carefully with try-catch for NumberFormatException
  • Provide clear usage instructions on error

Q2: Viva Preparation​

Max Marks: 5

Potential Viva Questions
  1. Q: What is the purpose of the args parameter in main method?

    • A: It stores command line arguments passed when running the program. args[0] is first argument, args.length gives count.
  2. Q: What happens if we try to access args[5] when only 3 arguments are passed?

    • A: It throws ArrayIndexOutOfBoundsException because the array doesn't have that index.
  3. Q: How do we convert String to int in Java?

    • A: Use Integer.parseInt(String) method. It throws NumberFormatException if string is not a valid number.
  4. Q: What is the time complexity of Arrays.sort()?

    • A: O(n log n) on average. It uses a tuned quicksort algorithm.
  5. Q: What is the difference between Bubble Sort and Selection Sort?

    • A: Bubble Sort repeatedly swaps adjacent elements; Selection Sort finds minimum and places it at correct position. Both are O(n²)."
  6. Q: Can we pass floating-point numbers as command line arguments?

    • A: Yes, use Double.parseDouble() to convert them.
  7. Q: Why should we validate command line arguments?

    • A: To prevent ArrayIndexOutOfBoundsException and NumberFormatException, and to provide helpful error messages.
Common Pitfalls
  • Not checking args.length: Always verify correct number of arguments before accessing
  • Forgetting NumberFormatException: Parse operations can fail - always use try-catch
  • Off-by-one errors: Array indices start at 0, be careful with loops
  • Not providing usage help: Show clear instructions when arguments are missing
  • Modifying original data: Keep original array if needed for display

Quick Navigation​

SetLink
Set ASolutions
Set BSolutions
Set CSolutions
Set DCurrent Page
Set ESolutions
Set FSolutions

Last Updated: April 2026

šŸ“ Visit Us

šŸ« VD Computer Tuition Surat

VD Computer Tuition
šŸ“ Address
2/66 Faram Street, Rustompura
Surat – 395002, Gujarat, India
šŸ“ž Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

Adajan•Althan•Amroli•Athwa•Athwalines•Bhagal•Bhatar•Bhestan•Canal Road•Chowk•Citylight•Dumas•Gaurav Path•Ghod Dod Road•Hazira•Jahangirpura•Kamrej•Kapodra•Katargam•Limbayat•Magdalla•Majura Gate•Mota Varachha•Nanpura•New Citylight•Olpad•Pal•Pandesara•Parle Point•Piplod•Puna•Rander•Ring Road•Rustampura•Sachin•Salabatpura•Sarthana•Sosyo Circle•Udhna•Varachha•Ved Road•Vesu•VIP Road
šŸ“ž Call SiršŸ’¬ WhatsApp Sir