💻 Programming Questions and Answers in C++ and Java for Freshers
🌟 Introduction
When you start preparing for a programming interview, you’ll often face practical coding questions that test your logic, syntax understanding, and problem-solving skills.
In this guide, you’ll learn some common programming questions in both C++ and Java, written in an easy-to-understand way.
Each question includes:
- A brief explanation
- A sample program
- And a clear output
🧩 Part 1: C++ Programming Questions and Answers
1. Write a program to check whether a number is even or odd.
Explanation:
If a number is divisible by 2, it is even; otherwise, it’s odd.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0)
cout << num << " is Even.";
else
cout << num << " is Odd.";
return 0;
}
Output:
Enter a number: 5
5 is Odd.
2. Find the sum of first N natural numbers.
Logic:
Sum = n*(n+1)/2
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << "Enter n: ";
cin >> n;
for(int i = 1; i <= n; i++) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}
Output:
Enter n: 5
Sum = 15
3. Check if a number is Prime.
Explanation:
A prime number is divisible only by 1 and itself.
#include <iostream>
using namespace std;
int main() {
int num, flag = 0;
cout << "Enter number: ";
cin >> num;
if (num <= 1) flag = 1;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
cout << num << " is Prime.";
else
cout << num << " is not Prime.";
return 0;
}
4. Reverse a string without using library function.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
for (int i = str.length() - 1; i >= 0; i--) {
cout << str[i];
}
return 0;
}
Example:
Input: Hello
Output: olleH
5. Swap two numbers without using a third variable.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
a = a + b;
b = a - b;
a = a - b;
cout << "After swapping: a = " << a << ", b = " << b;
return 0;
}
Output:
Enter two numbers: 5 10
After swapping: a = 10, b = 5
6. Find factorial of a number using recursion.
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;
}
7. Print Fibonacci Series up to n terms.
#include <iostream>
using namespace std;
int main() {
int n, a = 0, b = 1, next;
cout << "Enter number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; i++) {
cout << a << " ";
next = a + b;
a = b;
b = next;
}
return 0;
}
Output:
Enter number of terms: 6
Fibonacci Series: 0 1 1 2 3 5
☕ Part 2: Java Programming Questions and Answers
1. Check if a number is even or odd.
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num % 2 == 0)
System.out.println(num + " is Even.");
else
System.out.println(num + " is Odd.");
}
}
2. Find the factorial of a number.
import java.util.Scanner;
public class Factorial {
static int fact(int n) {
if (n == 0)
return 1;
else
return n * fact(n - 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
System.out.println("Factorial = " + fact(n));
}
}
3. Reverse a string without using built-in functions.
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.next();
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
System.out.println("Reversed string: " + rev);
}
}
4. Check if a number is Prime.
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
boolean flag = false;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is Prime.");
else
System.out.println(num + " is not Prime.");
}
}
5. Print Fibonacci Series.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms: ");
int n = sc.nextInt();
int a = 0, b = 1, next;
System.out.print("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
System.out.print(a + " ");
next = a + b;
a = b;
b = next;
}
}
}
6. Swap two numbers without using third variable.
import java.util.Scanner;
public class SwapNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swapping: a = " + a + ", b = " + b);
}
}
7. Check Palindrome String.
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
String str = sc.next();
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
if (str.equals(rev))
System.out.println("Palindrome!");
else
System.out.println("Not Palindrome!");
}
}
🧠 Bonus Tips for Freshers
- Always understand the logic before coding.
- Practice using loops, conditionals, and arrays.
- Revise OOP concepts like inheritance and polymorphism.
- Don’t memorize code — focus on how it works.
- Practice in both C++ and Java since logic remains same, only syntax changes.
🏁 Conclusion
These are some of the most commonly asked C++ and Java programming questions for freshers.
Each program here is simple, unique, and easy to understand.
If you master these basics, you’ll be ready for any beginner-level coding interview with confidence.
Great article! Thinking about how quickly visual content is evolving…tools like AI Agent could be game-changers for tournament prep – visualizing hands & reads! Accessible on any device is key for road warriors.
Popbra RTP, eh? So, we’re talkin’ slots, are we? Gotta see if those RTPs are juicy enough to spin! Don’t wanna waste my money on tight machines. popbra rtp
Downloaded the 87aapp. Simple and clean UI, easy to use. Makes betting on the move simple and fun! Give it a try: 87aapp
Alright, so I gave 116betcom a try. Honestly, it wasn’t bad at all! Pretty slick interface and easy to navigate. You might give it a go yourself! 116betcom
Hey, 6rbet1’s pretty solid! Found some interesting game offers and the user interface is pretty straightforward and easy to use. You can visit them through this link: 6rbet1
Anyone know where to download the Lucky Bet app for Luckybetbrasil.com? I’m hoping someone can find it for me at lucky bet app download
Needed the playtime download and it was easy to find. Downloaded quickly, no problems. Cheers! playtime download
Looking for a new online casino? Dafabet online casino might be worth checking out. Hear good things about their game selection. Explore it here: dafabet online casino