
Most online coding tests for freshers start with a round of short technical MCQs before the real programming problems: "what does this code print?", "what is the time complexity of this loop?", "which data structure fits here?". These questions look easy but are full of small traps. This coding test online practice set gives you 32 such questions in C++, Java, Python and JavaScript, each tested by running the code, with a clear explanation of why the answer is what it is.
How to practise: do not run the code yourself first. Read it, predict the output on paper, then tap your answer. Getting into the habit of tracing code by hand is exactly what an online coding test checks.
Topics covered in this set
- Integer division, modulo and overflow
- String concatenation and comparison in Java
- Arrays, references and loops in C++
- Bitwise operators
- Recursion
- Time complexity of loops
- Choosing the right data structure
- Sorting algorithm complexity
- Python and JavaScript output traps
- A timed 35-minute test format
How to solve output and complexity questions
Output-prediction questions are not about memorising rules. They test whether you can slow down and follow the code exactly the way the compiler does. A few habits make a big difference.
1. Check the types first
Before you calculate anything, look at the types. Dividing two int values throws away the decimal part, so 7 / 2 is 3, not 3.5. In Java, a char used in arithmetic turns into its number, so 'a' + 1 is 98. Many wrong answers come from skipping this step.
2. Evaluate left to right, one operator at a time
In Java, the + operator is evaluated left to right. While both sides are numbers it adds; once one side is a String, every + after that joins text. Writing down the intermediate value after each step stops you from mixing the two.
3. Trace loops with a small table
For any loop, write the value of the loop variable on each pass and stop when the condition fails. For complexity, ask how the loop variable changes. If it goes up by 1 each time, the loop runs n times. If it doubles or halves, it runs about log n times. A loop inside another loop usually multiplies the counts.
4. Know the classic traps
Comparing Java strings with == checks whether they are the same object, not whether the text matches. Signed integers wrap around when they overflow in Java. In C++ and Java, the result of % takes the sign of the left operand. If a question looks too easy, it is usually testing one of these.
Take it as a timed online coding test
Want it to feel like the real thing? Treat this page as a free online coding test. Set a timer, answer every question before you check, and only then open the explanations.
| Level | Questions | Time | Good score |
|---|---|---|---|
| Warm-up (C++ and Java basics) | Q1–Q10 | 10 minutes | 8/10 |
| Medium (traps, complexity, data structures) | Q11–Q20 | 12 minutes | 7/10 |
| Python and JavaScript | Q21–Q32 | 12 minutes | 9/12 |
| Full test | All 32 | 35 minutes | 24/32 |
Most campus and company tests give you about a minute per output question, so 35 minutes for 32 questions is close to the real pressure. If you score below the "good score", don't just reread the answers: redo the questions you missed the next day without looking.
Coding test practice questions with answers: C++ and Java (Q1–Q20)
Q1. What is the output of this Java code?
int a = 7 / 2; System.out.println(a);
a) 3.5
b) 3
c) 4
d) Compilation error
Q2. What is the output of this Java code?
System.out.println(10 + 20 + "Hi" + 10 + 20);
a) 1020Hi1020
b) 30Hi30
c) 30Hi1020
d) 1020Hi30
Q3. What is the output of this C++ code?
int arr[5] = {1, 2};
cout << arr[3];
a) 0
b) 2
c) A garbage value
d) Compilation error
Q4. What is the output of this C++ code?
for (int i = 0; i < 5; i += 2)
cout << i;
a) 01234
b) 0246
c) 135
d) 024
Q5. What is the output of this Java code?
String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1 == s2);
a) true
b) false
c) abc
d) Compilation error
Q6. What is the output of this C++ code?
int x = 10; int &r = x; r = 20; cout << x;
a) 10
b) 30
c) 20
d) An address
Q7. What is the output of this Java code?
int x = Integer.MAX_VALUE; x = x + 1; System.out.println(x);
a) -2147483648
b) 2147483648
c) ArithmeticException
d) 0
Q8. What is the output of this C++ code?
cout << (5 & 3) << (5 | 3) << (5 ^ 3);
a) 8125
b) 1 7 6
c) 726
d) 176
Q9. What does f(5) return?
int f(int n) {
if (n <= 1) return 1;
return n * f(n - 1);
}
a) 15
b) 120
c) 24
d) Infinite recursion
Q10. What is the output of this C++ code?
int a = 10, b = 3; cout << a / b << " " << a % b;
a) 3.33 1
b) 3 0
c) 3 1
d) 1 3
Q11. What does the expression -7 % 3 evaluate to in C++ and Java?
a) -1
b) 2
c) 1
d) -2
Q12. What does count(10) return?
int count(int n) {
int c = 0;
while (n) {
c += n & 1;
n >>= 1;
}
return c;
}
a) 4
b) 10
c) 1
d) 2
Q13. What is the time complexity of this code?
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
sum++;
a) O(n)
b) O(n log n)
c) O(n²)
d) O(n³)
Q14. What is the time complexity of this loop?
for (int i = 1; i < n; i *= 2)
cout << i;
a) O(n)
b) O(log n)
c) O(√n)
d) O(n²)
Q15. Which data structure is best for checking whether brackets in an expression like {[()]} are balanced?
a) Stack
b) Queue
c) Binary search tree
d) Heap
Q16. Which data structure is used to implement Breadth-First Search (BFS) on a graph?
a) Stack
b) Priority queue only
c) Queue
d) Linked list only
Q17. Which sorting algorithm has a worst-case time complexity of O(n log n)?
a) Quick sort
b) Bubble sort
c) Insertion sort
d) Merge sort
Q18. What is the output of this Java code?
System.out.println('a' + 1);
a) b
b) 98
c) a1
d) Compilation error
Q19. What is the output of this Java code?
char c = 'A'; c += 2; System.out.println(c);
a) C
b) 67
c) A2
d) Compilation error
Q20. In the Two Sum problem (find two numbers in an unsorted array that add up to a target), what is the best time complexity using a hash map?
a) O(n²)
b) O(n log n)
c) O(n)
d) O(1)
Python and JavaScript coding test questions (Q21–Q32)
A lot of online coding tests now let you pick Python or JavaScript. These questions cover the traps that catch people who usually write C++ or Java. Every answer was checked by running the code (Python 3 and Node.js).
Q21. What is the output of this Python code?
print(7 // 2, -7 // 2)
a) 3 -3
b) 3 -4
c) 3.5 -3.5
d) 4 -4
Q22. What is the output of this Python code?
a = [1, 2, 3] b = a b.append(4) print(len(a))
a) 3
b) Error
c) 4
d) 7
Q23. What is the output of this Python code?
def f(x, lst=[]):
lst.append(x)
return lst
f(1)
print(f(2))
a) [1, 2]
b) [2]
c) [1]
d) Error
Q24. What is the output of this Python code?
print("abc"[::-1])
a) abc
b) a
c) Error
d) cba
Q25. What is the output of this Python code?
print(0.1 + 0.2 == 0.3)
a) True
b) False
c) 0.3
d) Error
Q26. What is the output of this Python code?
print([i * i for i in range(4) if i % 2])
a) [0, 4]
b) [0, 1, 4, 9]
c) [1, 9]
d) [1, 3]
Q27. What is the output of this JavaScript code?
console.log(typeof null);
a) object
b) null
c) undefined
d) number
Q28. What is the output of this JavaScript code?
console.log("5" - 2, "5" + 2);
a) 3 7
b) 52 52
c) NaN 52
d) 3 52
Q29. What is the output of this JavaScript code?
console.log([1, 2, 3].map(parseInt));
a) [1, 2, 3]
b) [1, NaN, NaN]
c) [NaN, NaN, NaN]
d) [1, 2, NaN]
Q30. What does this JavaScript code print?
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i));
}
a) 0 1 2
b) 0 0 0
c) 3 3 3
d) 2 2 2
Q31. What is the output of this JavaScript code?
const arr = [3, 1, 10]; arr.sort(); console.log(arr);
a) [1, 10, 3]
b) [1, 3, 10]
c) [10, 3, 1]
d) [3, 1, 10]
Q32. In Python, what is the average time complexity of x in my_list and x in my_set?
a) O(1) and O(1)
b) O(log n) and O(1)
c) O(n) and O(log n)
d) O(n) and O(1)
Quick revision notes
- int / int drops the decimal part: 7 / 2 = 3.
- Java + goes left to right: numbers add until a String appears, then everything joins as text.
- Compare Java strings with equals(), not ==.
- Java int overflow wraps around silently: MAX_VALUE + 1 = MIN_VALUE.
- % takes the sign of the left operand: -7 % 3 = -1.
- A partly initialised C++ array fills the rest with 0.
- Loop variable +1 each pass → O(n); ×2 or ÷2 each pass → O(log n); nested → multiply.
- Brackets → stack. BFS → queue. DFS → stack or recursion. Fast lookup → hash map.
- Merge sort is O(n log n) in the worst case; quick sort is O(n²) in the worst case.
Frequently asked questions
What kind of questions come in an online coding test?
Most fresher coding tests have two parts: a set of technical MCQs like the ones here (output prediction, complexity, data structures) and one to three programming problems you solve in an online editor. Practising both parts matters, because the MCQ round often decides who reaches the coding round.
Which language should I use for coding tests, C++ or Java?
Use the language you are most comfortable with. C++ is popular for its speed and STL, and Java is widely accepted too. The questions on this page cover both because tests often show code in either language.
Were these answers checked?
Yes. Every code snippet on this page was compiled and run to confirm the output before publishing.
Is this online coding test free?
Yes. All 32 questions, answers and explanations are free and need no sign-up. Use it as a timed mock before a placement or company coding test, then practise the topics you got wrong.
How can I get faster at output questions?
Trace code by hand every day for a couple of weeks. Write down the value of each variable after every line. Once tracing becomes a habit, you will spot traps like integer division and string comparison almost instantly.
What to practise next
Now move on to full programming problems. Our Coding Problems and Solutions page collects solved problems from TCS CodeVita, LeetCode and more. To build the concepts behind them, follow the Data Structures and Algorithms Hub, and brush up on language details in the C and C++ Programming Hub and the Java Programming Hub.
Comments
Post a Comment