Technical Interview Questions with Code Examples (C,
C++, Python, Java)
C Language - Questions & Code Examples
1. Call by value vs Call by reference (simulate with pointers in C):
#include <stdio.h>
void call_by_value(int x) {
x = 100; // modifies only local copy
}
void call_by_reference(int *p) {
*p = 100; // modifies caller's variable
}
int main() {
int a = 10;
call_by_value(a);
printf("After call_by_value: %d\n", a); // prints 10
call_by_reference(&a);
printf("After call_by_reference: %d\n", a); // prints 100
return 0;
}
2. malloc() vs calloc() example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *a = (int*) malloc(5 * sizeof(int)); // uninitialized memory
int *b = (int*) calloc(5, sizeof(int)); // zero-initialized memory
for (int i=0;i<5;i++) {
printf("malloc a[%d]=%d, calloc b[%d]=%d\n", i, a[i], i, b[i]);
}
free(a); free(b);
return 0;
}
C++ Language - Questions & Code Examples
1. Basic OOP with inheritance & virtual functions (polymorphism):
#include <iostream>
using namespace std;
class Animal {
public:
virtual void speak() { cout << "Animal sound\n"; } // virtual -> runtime polymorphism
virtual ~Animal() {}
};
class Dog : public Animal {
public:
void speak() override { cout << "Woof!\n"; }
};
int main() {
Animal* a = new Dog();
a->speak(); // prints "Woof!" because of virtual dispatch
delete a;
return 0;
}
2. Constructor and Destructor example:
#include <iostream>
using namespace std;
class Resource {
public:
Resource() { cout << "Acquired\n"; }
~Resource() { cout << "Released\n"; }
};
int main() {
{ Resource r; } // constructor then destructor called at scope end
return 0;
}
Python Language - Questions & Code Examples
1. Decorator example:
def debug(fn):
def wrapper(*args, **kwargs):
print(f"Calling {fn.__name__} with", args, kwargs)
result = fn(*args, **kwargs)
print(f"{fn.__name__} returned", result)
return result
return wrapper
@debug
def add(a, b):
return a + b
add(2, 3)
2. Generator example:
def gen(n):
for i in range(n):
yield i*i # lazy sequence of squares
for x in gen(5):
print(x)
3. *args and **kwargs example:
def func(*args, **kwargs):
print("args:", args)
print("kwargs:", kwargs)
func(1,2, a=3, b=4)
Java Language - Questions & Code Examples
1. Thread example using Runnable:
class MyTask implements Runnable {
public void run() {
[Link]("Thread running: " + [Link]().getName());
}
public static void main(String[] args) {
Thread t = new Thread(new MyTask());
[Link]();
}
}
2. Interface vs Abstract class (simple example):
interface Shape {
double area();
}
abstract class BaseShape {
String name;
BaseShape(String n) { name = n; }
abstract double area();
}
class Circle extends BaseShape implements Shape {
double r;
Circle(double r) { super("circle"); this.r = r; }
double area() { return [Link] * r * r; }
}
3. HashMap vs Hashtable (note: Hashtable is synchronized):
import [Link];
import [Link];
public class MapExample {
public static void main(String[] args) {
HashMap<String,Integer> hm = new HashMap<>();
Hashtable<String,Integer> ht = new Hashtable<>();
[Link]("a", 1);
[Link]("a", 1);
[Link]("HashMap allows null keys: " + [Link](null)); // false here, bu
}
}