iExamWorld
Notes

Programming Fundamentals: C, Java and Python Essentials for Placement Tests

English · Free on iExamWorld

Why programming fundamentals matter in placement tests

Recruiters such as TCS, Accenture, Capgemini, Cognizant, Wipro and Infosys, and assessment platforms such as AMCAT, regularly include a section on programming basics. You usually read a few lines of C, Java or Python and choose the output, or pick the correct statement about an OOP idea. Speed comes from knowing a small set of rules very well. This note collects those rules in one place.

Data types and sizes

In C the size of most types depends on the compiler and platform. Placement questions therefore state an assumption, and the usual one is that int is 4 bytes. Under the common assumptions, char is 1 byte, int is 4 bytes, float is 4 bytes and double is 8 bytes. In Java the sizes are fixed by the language itself: byte 1, short 2, char 2, int 4, long 8, float 4 and double 8 bytes. Python integers have no fixed size; they grow as large as memory allows.

TypeC (typical assumption)Java (fixed)
char1 byte2 bytes
int4 bytes4 bytes
float4 bytes4 bytes
double8 bytes8 bytes

The sizeof operator in C applied to an array gives the size of the whole array. For int arr[10]; with a 4-byte int, sizeof(arr) is 40. For char s[] = "hello"; sizeof(s) is 6 because the terminating null character is stored, while strlen(s) is 5.

Operators, precedence and integer arithmetic

Multiplication, division and modulo bind more tightly than addition and subtraction, and operators of equal precedence are evaluated left to right. So 2 + 3 * 4 % 5 is evaluated as 2 + ((3 * 4) % 5), which is 2 + 2 = 4.

Integer division is the most common trap. In C (C99) and Java, dividing two integers truncates toward zero, so 7 / 2 is 3 and -7 / 2 is -3. The remainder takes the sign of the dividend, so -7 % 2 is -1. Python 3 behaves differently:

  • 7 / 2 is 3.5, because / always gives a float in Python 3.
  • 7 // 2 is 3, and -7 // 2 is -4, because // is floor division and rounds toward negative infinity.
  • -7 % 2 is 1, because in Python the remainder takes the sign of the divisor.

Increment operators

In C and Java, a++ uses the current value and then increments, while ++a increments first and then uses the new value. After int a = 5; int b = a++ + 2; the value of b is 7 and a is 6. With int a = 5; int b = ++a + 2; both changes happen first, so b is 8 and a is 6. Avoid expressions such as i = i++ or a++ + a++ in C: modifying the same variable twice without a sequence point is undefined behaviour, and a good test will not ask for its output. Python has no ++ operator at all; you write i += 1.

Loops and output tracing

For any loop, write down the variables in a small table and update it one pass at a time. For example, in Java, int s = 0; for (int i = 1; i <= 10; i += 3) s += i; gives i the values 1, 4, 7 and 10, so s ends at 22. Check three things every time: the starting value, the exact condition (i < n or i <= n), and the step. Most wrong answers come from running the loop one time too many or too few.

Functions and recursion

A recursive function needs a base case that stops the calls and a recursive case that moves toward the base case. To trace one, expand the calls on paper. For int f(int n) { if (n <= 1) return 1; return n * f(n - 2); }, the call f(7) expands to 7 * 5 * 3 * 1 = 105. When the print statement comes after the recursive call, values appear in reverse order of the calls, because each call finishes only after the deeper calls return.

Arrays, strings and pointers in C

Array indexes start at 0, so an array of size n has valid indexes 0 to n - 1. A C string is a char array ending with the null character. In C, the name of an array used in an expression converts to a pointer to its first element.

  • int x = 5; int *p = &x; *p = *p * 3; changes x to 15, because *p refers to x itself.
  • int a[] = {10, 20, 30, 40}; int *p = a; p++; makes p point to a[1], so *p is 20 and *p + 1 is 21.
  • *(a + i) and a[i] mean the same element.
  • Pointer arithmetic moves by whole elements, not bytes: p + 1 points to the next int.

Object-oriented programming

A class is a blueprint that defines fields and methods; an object is an instance created from it, for example with new in Java. The four pillars are tested repeatedly.

  • Encapsulation: keeping fields private and exposing controlled access through public methods such as getters and setters.
  • Inheritance: a subclass reuses and extends a superclass using extends in Java. Java does not allow a class to extend more than one class.
  • Polymorphism: one name, many forms. Overloading means same method name with different parameter lists in the same class and is resolved at compile time. Overriding means a subclass redefines a superclass method with the same signature and is resolved at run time.
  • Abstraction: showing what an object does while hiding how, through abstract classes and interfaces.
PointOverloadingOverriding
WhereSame classSuperclass and subclass
ParametersMust differMust be the same
ResolvedCompile timeRun time

If class B extends A and both define show(), then A obj = new B(); obj.show(); runs the version in B. The reference type decides what can be called; the object type decides which overridden version runs.

Interfaces and abstract classes

An abstract class may have constructors, fields and both abstract and concrete methods, and it cannot be instantiated directly. An interface has no constructors, and its fields are implicitly public, static and final. A Java class extends one class but can implement many interfaces, using the implements keyword. Since Java 8, interfaces may also contain default and static methods.

Constructors

A constructor has the same name as the class and no return type. If you write no constructor, Java supplies a default no-argument one. Constructors can be overloaded. When a subclass object is created, the superclass constructor runs first, because the subclass constructor begins with a call to super(), either written or implicit. So creating a C object where C extends P prints the P message before the C message.

Static members

A static field belongs to the class, not to any object, so all objects share one copy. A constructor that does count++ on a static count will show 3 after three objects are created. A static method can be called using the class name and cannot use this or access instance fields directly.

Exceptions

In Java, code that may fail goes in a try block, handlers go in catch blocks, and a finally block runs whether or not an exception occurs. Integer division by zero throws ArithmeticException, while floating-point division by zero gives Infinity instead. In try { int x = 10 / 0; print A } catch (ArithmeticException e) { print B } finally { print C }, the output is BC, because the statement after the failing line is skipped. Python uses try, except, else and finally in the same way; dividing by zero raises ZeroDivisionError.

Python collections, slicing and mutability

  • A list, such as [1, 2, 3], is ordered and mutable.
  • A tuple, such as (1, 2, 3), is ordered and immutable; t[0] = 9 raises a TypeError.
  • A dict maps keys to values; d.get(key, default) returns the default when the key is missing, while d[key] raises a KeyError.
  • Assignment such as b = a does not copy a list. Both names refer to the same object, so b.append(4) also changes a. Use a.copy() or a[:] for a shallow copy.

Slicing uses s[start:stop:step], where stop is excluded. For s = "PLACEMENT", s[1:6:2] takes indexes 1, 3 and 5 and gives "LCM", and s[::-1] gives the string reversed. Negative indexes count from the end, so s[-1] is "T".

Java String immutability

A Java String object can never change. Methods such as concat, toUpperCase and replace return a new String. So String s = "abc"; s.concat("def"); leaves s as "abc"; you must write s = s.concat("def") to keep the result. For repeated changes, StringBuilder is the mutable alternative. Also, == compares references, while equals compares characters.

Quick revision points

  • Assume int is 4 bytes in C unless the question says otherwise; Java sizes are fixed, and Java char is 2 bytes.
  • sizeof of an array gives the whole array size; sizeof of a string literal array includes the null character.
  • C and Java integer division truncates toward zero; Python // floors toward negative infinity.
  • In C and Java, % takes the sign of the dividend; in Python, it takes the sign of the divisor.
  • * / % bind more tightly than + and -, and equal precedence goes left to right.
  • a++ uses then increments; ++a increments then uses; never trust i = i++ in C.
  • Trace loops with a variable table and check the boundary condition carefully.
  • Every recursion needs a base case; output after the recursive call appears in reverse order.
  • p++ on an int pointer moves to the next element; *p reads or writes the pointed variable.
  • Overloading is compile time; overriding is run time, decided by the object type.
  • The superclass constructor runs before the subclass constructor.
  • Static fields are shared by all objects of a class.
  • finally runs whether or not an exception is thrown.
  • Lists and dicts are mutable; tuples and strings are immutable.
  • Java String methods return new strings; the original never changes.

These notes were written with AI assistance for iExamWorld, based on the official syllabus. Spotted a mistake? Tell us.