AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started

Topic

Programming Fundamentals aptitude questions

Operators, control flow, functions, pointers and output prediction in C, Java and Python.

0 of 40 solved in this view

Formula sheetProgramming Fundamentals: the essentialsFormula sheetProgramming Fundamentals: the essentialsShow

Output questions test a handful of rules that differ between languages.

C and Java

  • Integer division truncates towards zero: 7 / 2 is 3, -7 / 2 is −3 and -7 % 2 is −1. One floating operand gives real division: 7 / 2.0 is 3.5.
  • Precedence, high to low: unary, then * / %, then + -, shifts, relational, equality, bitwise, &&, ||, ?: and assignment. Equal operators group left to right, except ?: and assignment.
  • i++ yields the old value, ++i the new one. && and || short-circuit, so a side effect on the right may never happen. A switch falls through until a break.
  • C: arguments are copied, so a swap needs pointers; *(p + i) is p[i]; an array parameter is a pointer, so sizeof gives the pointer's size; strlen omits the terminating null that sizeof counts; a static local keeps its value between calls.
  • Java: == compares references and equals contents; strings are immutable; int overflow wraps, so Integer.MAX_VALUE + 1 is Integer.MIN_VALUE; 1 + 2 + "3" is 33; finally runs even after a return.

Python

  • 7 / 2 is 3.5, 7 // 2 is 3 and -7 // 2 is −4: floor division rounds towards minus infinity.
  • b = a shares one list and a[:] copies it; [[0] * 3] * 2 holds one inner list twice.
  • A mutable default argument is shared between calls, and a loop's else runs only when no break fired.

Questions40

1

Two kinds of division in C

What does this C program print?

c · operators

60s

Easy
2

Pre-increment against post-increment

What does this C program print?

c · increment

75s

Medium
3

Precedence of the arithmetic operators

What does this C program print?

c · precedence

75s

Medium
4

Short-circuit evaluation

What does this C program print?

c · short circuit

90s

Medium
5

A static local variable

What does this C program print?

c · static · scope

90s

Medium
6

A swap that does not swap

What does this C program print?

c · call by value

60s

Easy
7

Pointer arithmetic against value arithmetic

What does this C program print?

c · pointers

90s

Medium
8

sizeof on an array parameter

Assume a machine where int occupies 4 bytes and a pointer occupies 8 bytes. What does this C program print?

c · pointers · sizeof

120s

Hard
9

strlen against sizeof

What does this C program print?

c · strings · sizeof

75s

Medium
10

Falling through a switch

What does this C program print?

c · switch

60s

Easy
11

A nested conditional expression

What does this C program print?

c · ternary

75s

Medium
12

Comparing a signed value with an unsigned one

What does this C program print?

c · type promotion

105s

Hard
13

A character used in arithmetic

Assume the ASCII character set, in which 'A' has the value 65. What does this C program print?

c · characters · printf

60s

Easy
14

Subscripting written backwards

What does this C program print?

c · arrays · pointers

90s

Hard
15

A local that hides a global

What does this C program print?

c · scope

60s

Easy
16

Dividing a negative number

What does this C program print? Assume a C99 or later compiler.

c · operators

90s

Medium
17

Comparing strings with == and equals

What does this Java program print?

java · strings

75s

Easy
18

Comparing boxed integers

What does this Java program print?

java · autoboxing

105s

Hard
19

String methods return new strings

What does this Java program print?

java · strings · immutability

60s

Easy
20

Integer and floating-point division in Java

What does this Java program print?

java · operators

60s

Easy
21

When + means concatenation

What does this Java program print?

java · strings · operators

75s

Medium
22

A static field shared by every object

What does this Java program print?

java · static

90s

Medium
23

What a method can and cannot change

What does this Java program print?

java · call by value · arrays

90s

Medium
24

String against StringBuilder as a parameter

What does this Java program print?

java · strings · immutability

105s

Medium
25

The type of a conditional expression

What does this Java program print?

java · ternary · type promotion

105s

Hard
26

Grouped labels and a missing break

What does this Java program print?

java · switch

90s

Medium
27

finally runs after the return value is fixed

What does this Java program print?

java · exceptions

90s

Hard
28

Adding one to the largest int

What does this Java program print?

java · overflow

75s

Medium
29

Three divisions in Python

What does this Python program print?

python · operators

75s

Medium
30

Aliasing against slicing

What does this Python program print?

python · lists · mutability

90s

Medium
31

A default argument that remembers

What does this Python program print?

python · functions · mutability

105s

Hard
32

Python strings are immutable too

What does this Python program print?

python · strings · immutability

60s

Easy
33

Building a grid with the repetition operator

What does this Python program print?

python · lists · aliasing

105s

Hard
34

Equality against identity

What does this Python program print?

python · identity

60s

Easy
35

Three slices of one string

What does this Python program print?

python · strings · slicing

90s

Medium
36

The else clause of a for loop

What does this Python program print?

python · loops

105s

Hard
37

Mutating against rebinding

What does this Python program print?

python · functions · mutability

90s

Medium
38

Assignment makes a name local

What does this Python program print?

python · scope

60s

Easy
39

A generator and a comprehension in one line

What does this Python program print?

python · comprehensions

90s

Medium
40

Short-circuiting past a function call

What does this Python program print?

python · short circuit

90s

Medium

Showing 40 of 40

Programming Fundamentals Aptitude Questions with Solutions

40 Programming Fundamentals questions with hints, worked solutions and time targets — 11 easy · 20 medium · 9 hard.

  • Section: Programming MCQs
  • Questions: 40
  • By difficulty: 11 easy · 20 medium · 9 hard
  • Cost: Free, unlimited on every plan

Operators, control flow, functions, pointers and output prediction in C, Java and Python.

Programming Fundamentals: the essentials

Output questions test a handful of rules that differ between languages.

C and Java

  • Integer division truncates towards zero: 7 / 2 is 3, -7 / 2 is −3 and -7 % 2 is −1. One floating operand gives real division: 7 / 2.0 is 3.5.
  • Precedence, high to low: unary, then * / %, then + -, shifts, relational, equality, bitwise, &&, ||, ?: and assignment. Equal operators group left to right, except ?: and assignment.
  • i++ yields the old value, ++i the new one. && and || short-circuit, so a side effect on the right may never happen. A switch falls through until a break.
  • C: arguments are copied, so a swap needs pointers; *(p + i) is p[i]; an array parameter is a pointer, so sizeof gives the pointer's size; strlen omits the terminating null that sizeof counts; a static local keeps its value between calls.
  • Java: == compares references and equals contents; strings are immutable; int overflow wraps, so Integer.MAX_VALUE + 1 is Integer.MIN_VALUE; 1 + 2 + "3" is 33; finally runs even after a return.

Python

  • 7 / 2 is 3.5, 7 // 2 is 3 and -7 // 2 is −4: floor division rounds towards minus infinity.
  • b = a shares one list and a[:] copies it; [[0] * 3] * 2 holds one inner list twice.
  • A mutable default argument is shared between calls, and a loop's else runs only when no break fired.

Questions

  1. Two kinds of division in C Easy
    What does this C program print?
  2. Pre-increment against post-increment Medium
    What does this C program print?
  3. Precedence of the arithmetic operators Medium
    What does this C program print?
  4. Short-circuit evaluation Medium
    What does this C program print?
  5. A static local variable Medium
    What does this C program print?
  6. A swap that does not swap Easy
    What does this C program print?
  7. Pointer arithmetic against value arithmetic Medium
    What does this C program print?
  8. sizeof on an array parameter Hard
    Assume a machine where int occupies 4 bytes and a pointer occupies 8 bytes. What does this C program print?
  9. strlen against sizeof Medium
    What does this C program print?
  10. Falling through a switch Easy
    What does this C program print?
  11. A nested conditional expression Medium
    What does this C program print?
  12. Comparing a signed value with an unsigned one Hard
    What does this C program print?
  13. A character used in arithmetic Easy
    Assume the ASCII character set, in which 'A' has the value 65. What does this C program print?
  14. Subscripting written backwards Hard
    What does this C program print?
  15. A local that hides a global Easy
    What does this C program print?
  16. Dividing a negative number Medium
    What does this C program print? Assume a C99 or later compiler.
  17. Comparing strings with == and equals Easy
    What does this Java program print?
  18. Comparing boxed integers Hard
    What does this Java program print?
  19. String methods return new strings Easy
    What does this Java program print?
  20. Integer and floating-point division in Java Easy
    What does this Java program print?
  21. When + means concatenation Medium
    What does this Java program print?
  22. A static field shared by every object Medium
    What does this Java program print?
  23. What a method can and cannot change Medium
    What does this Java program print?
  24. String against StringBuilder as a parameter Medium
    What does this Java program print?
  25. The type of a conditional expression Hard
    What does this Java program print?
  26. Grouped labels and a missing break Medium
    What does this Java program print?
  27. finally runs after the return value is fixed Hard
    What does this Java program print?
  28. Adding one to the largest int Medium
    What does this Java program print?
  29. Three divisions in Python Medium
    What does this Python program print?
  30. Aliasing against slicing Medium
    What does this Python program print?
  31. A default argument that remembers Hard
    What does this Python program print?
  32. Python strings are immutable too Easy
    What does this Python program print?
  33. Building a grid with the repetition operator Hard
    What does this Python program print?
  34. Equality against identity Easy
    What does this Python program print?
  35. Three slices of one string Medium
    What does this Python program print?
  36. The else clause of a for loop Hard
    What does this Python program print?
  37. Mutating against rebinding Medium
    What does this Python program print?
  38. Assignment makes a name local Easy
    What does this Python program print?
  39. A generator and a comprehension in one line Medium
    What does this Python program print?
  40. Short-circuiting past a function call Medium
    What does this Python program print?

Placement tests that ask programming fundamentals