Most Common & Basic Questions asked in BCA Java Practical Exam
Sometimes the examiner asks very basic questions in Java practical to test the student’s core fundamentals. Examiners ask basic logical questions. They want to test how students deal with logical questions. These questions are simple, like swapping two numbers in Java, Find the greatest number in Java, and so on. Also students are confused, even though they don’t know how to solve.
Common questions asked in BCA Java Programming
- Swap two numbers in Java
- Find the Greatest number in Java
- Find the sum of digits of a number in Java.
- Make basic calculator app in Java
Let’s make all the above program steps by step in the easiest way. We will also know the logic and what approach we used in that program
1. Swap two numbers in Java
In this swap two number program, we are using a temporary variable. In the program, I used three variables X, Y, and Z. The Z is a temporary blank variable.
- Z stores Y,
- Y takes the value of X
- X takes the value of Z
- Then after swapped values will be
x = 20andy = 10.
public class Main {
public static void main(String[] args) {
int x = 10;
int y = 20;
int z;
System.out.println("Default Value: x = " + x + " y = " + y);
z = y;
y = x;
x = z;
System.out.println("After Swap : x = " + x + " y = " + y);
}
}
2. Find the Greatest number in Java
To find the largest number, we will create three variables A, B, and C and assign some values to them. Then, we will compare them one by one. The logic approach will be:
- If A is greater then B and A is greater then C
(a >= b && a >= c): Then A is larger number - If B is greater then A and B is greater then C
(b >= a && b >= c): Then B is larger number - If C is greater then A and C is greater then C
(c >= a && c >= b): Then C is larger number
public class Main {
public static void main(String[] args) {
int a = 90;
int b = 20;
int c = 115;
if (a >= b && a >= c) {
System.out.println("A is greatest : " + a);
}
else if (b >= a && b >= c) {
System.out.println("B is greatest : " + b);
} else if (c >= a && c >= b) {
System.out.println("C is greatest : " + c);
}
}
}
3. Find the sum of digits of a number in Java.
In this program, we will use the divide operator (/) and the modulo operator (%) to extract the single digit from the number. num = 55 is a static value; you can change this, or you can make it dynamic as well.
public class Main {
public static void main(String[] args) {
int num = 55;
int a = num / 10;
int b = num % 10;
int sum = a + b;
System.out.println("The Number is: " + num);
System.out.println("The sum of digits is " + a + " + " + b + " = " + sum);
}
}
4. Make basic calculator app in Java
I try to keep this calculator app simple. Below code snippet below, you can see, I’m taking three inputs from the user. First number, second number, and the arithmetic operator that one user wants to perform. In switch, I’m passing an arithmetic operator as a string
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("-------------Basic Calculator-----------------");
System.out.print("Enter the first number: ");
double num1 = input.nextDouble();
System.out.print("Enter the second number: ");
double num2 = input.nextDouble();
System.out.println("Choose an operation: ");
System.out.println("Use + Addition");
System.out.println("Use - Subtraction");
System.out.println("Use * Multiplication");
System.out.println("Use / Division");
String choice = input.next();
double result;
switch (choice) {
case "+":
result = num1 + num2;
System.out.println("Result: " + num1 + " + " + num2 + " = " + result);
break;
case "-":
result = num1 - num2;
System.out.println("Result: " + num1 + " - " + num2 + " = " + result);
break;
case "*":
result = num1 * num2;
System.out.println("Result: " + num1 + " * " + num2 + " = " + result);
break;
case "/":
if (num2 != 0) {
result = num1 / num2;
System.out.println("Result: " + num1 + " / " + num2 + " = " + result);
} else {
System.out.println("Error: Division by zero is not allowed.");
}
break;
default:
System.out.println("Enter only +, -, *, /.");
}
input.close();
}
}
Conclusion:
The main purpose of asking these basic-level questions is to test the fundamental core concepts of Java. That is why these basic level questions are asked in the exam.