InfyTQ Java questions

The InfyTQ syllabus topics: basics, OOP, arrays and strings, polymorphism, abstract classes and interfaces, and recursion.

1. What is the output?

public class Main {
  public static void main(String[] args) {
    int a = 7, b = 2;
    System.out.println(a / b + " " + a % b);
  }
}
  1. 3 1
  2. 3.5 1
  3. 4 1
  4. 3 0
Answer

A. 3 1

Dividing two ints gives an int (the fraction is dropped); % is the remainder.

2. What is the output?

public class Main {
  public static void main(String[] args) {
    System.out.println(1 + 2 + "3" + 4 + 5);
  }
}
  1. 15
  2. 3345
  3. 12345
  4. 3312
Answer

B. 3345

Left to right: 1 + 2 is 3, then string joining starts: "3" + "3" + "4" + "5".

3. What is the output?

public class Main {
  public static void main(String[] args) {
    char c = 'A';
    c += 2;
    System.out.println(c);
  }
}
  1. A2
  2. C
  3. 67
  4. B
Answer

B. C

char is a number underneath; adding 2 to 'A' gives 'C'.

4. What is the default value of an int field (instance variable) in a Java class?

  1. null
  2. 1
  3. 0
  4. A garbage value
Answer

C. 0

Fields get default values: 0 for numbers, false for boolean and null for objects. Local variables get none.

5. How many bits does an int use in Java?

  1. It depends on the operating system
  2. 64
  3. 16
  4. 32 on every platform
Answer

D. 32 on every platform

Java fixes the sizes of its primitive types: int is always 32 bits.

6. What is the output?

public class Main {
  public static void main(String[] args) {
    double d = 9.99;
    int i = (int) d;
    System.out.println(i);
  }
}
  1. 10
  2. 9.0
  3. 9.99
  4. 9
Answer

D. 9

Casting a double to int drops the fraction; it does not round.

7. What is the output?

public class Main {
  public static void main(String[] args) {
    int x = 5;
    int y = x++ + ++x;
    System.out.println(x + " " + y);
  }
}
  1. 7 11
  2. 6 11
  3. 7 12
  4. 7 13
Answer

C. 7 12

x++ gives 5 and makes x 6; ++x makes x 7 and gives 7; 5 + 7 = 12.

8. What is the output?

class Box {
  int w;
  Box(int w) { this.w = w; }
}
public class Main {
  public static void main(String[] args) {
    Box b = new Box(5);
    System.out.println(b.w * 2);
  }
}
  1. 0
  2. 10
  3. 25
  4. 5
Answer

B. 10

The constructor stores 5 in the field w.

9. What is the output?

class Item {
  static int made = 0;
  Item() { made++; }
}
public class Main {
  public static void main(String[] args) {
    new Item();
    new Item();
    new Item();
    System.out.println(Item.made);
  }
}
  1. 3
  2. 1
  3. 0
  4. 2
Answer

A. 3

A static field belongs to the class and is shared by every object.

10. Which keyword refers to the current object inside a method or constructor?

  1. current
  2. super
  3. self
  4. this
Answer

D. this

this is the object the method is running on.

11. If a class has no constructor written in it, Java:

  1. Adds a constructor taking every field
  2. Does not allow objects of the class
  3. Adds a static constructor
  4. Adds a default constructor with no arguments
Answer

D. Adds a default constructor with no arguments

The compiler supplies a no-argument constructor only when you write none.

12. Which access modifier makes a member visible only inside its own class?

  1. protected
  2. public
  3. No modifier (default)
  4. private
Answer

D. private

Key idea: private members can be used only inside the class that declares them.

Why not the others:

  • protected adds the package and subclasses.
  • public — default (no modifier) means visible within the same package.
  • No modifier (default) — public means visible everywhere.

Remember: private < default < protected < public.

13. What is the output?

class Box {
  int w;
  Box(int w) { this.w = w; }
}
public class Main {
  public static void main(String[] args) {
    Box a = new Box(1);
    Box b = a;
    b.w = 9;
    System.out.println(a.w);
  }
}
  1. 1
  2. 9
  3. 10
  4. 0
Answer

B. 9

b = a copies the reference, so both names point to the same object.

14. Encapsulation in Java is usually done with:

  1. all the code inside main
  2. private fields and public getter and setter methods
  3. public fields only
  4. only static methods
Answer

B. private fields and public getter and setter methods

Hiding fields and exposing methods controls how data is read and changed.

15. What is the output?

public class Main {
  public static void main(String[] args) {
    int[] a = {4, 7, 1, 9};
    int s = 0;
    for (int x : a) s += x;
    System.out.println(s + " " + a.length);
  }
}
  1. 21 3
  2. 21 4
  3. 20 4
  4. 9 4
Answer

B. 21 4

The loop adds 4 + 7 + 1 + 9; length is the number of elements.

16. What is the output?

public class Main {
  public static void main(String[] args) {
    String s = "placement";
    System.out.println(s.substring(2, 5));
  }
}
  1. ace
  2. lac
  3. lace
  4. acem
Answer

A. ace

substring(2, 5) takes indices 2, 3 and 4.

17. What is the output?

public class Main {
  public static void main(String[] args) {
    String a = "hi";
    String b = new String("hi");
    System.out.println((a == b) + " " + a.equals(b));
  }
}
  1. false true
  2. true true
  3. true false
  4. false false
Answer

A. false true

== compares references (new String makes a new object); equals compares the text.

18. What value does each element of a new int[5] start with?

  1. null
  2. 0
  3. Undefined
  4. A garbage value
Answer

B. 0

Array elements get default values; for int that is 0.

19. What is the output?

public class Main {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("abc");
    sb.reverse();
    System.out.println(sb);
  }
}
  1. cab
  2. bca
  3. abc
  4. cba
Answer

D. cba

StringBuilder is mutable and reverse() changes it in place.

20. What is the output?

public class Main {
  public static void main(String[] args) {
    String s = "banana";
    System.out.println(s.indexOf('n') + " " + s.lastIndexOf('a'));
  }
}
  1. 1 5
  2. 2 5
  3. 2 6
  4. 3 6
Answer

B. 2 5

Indices start at 0: the first n is at 2 and the last a is at 5.

21. What is the output?

public class Main {
  public static void main(String[] args) {
    int[][] m = {{1, 2}, {3, 4}};
    System.out.println(m[1][0] + m[0][1]);
  }
}
  1. 3
  2. 7
  3. 5
  4. 6
Answer

C. 5

m[1][0] is 3 and m[0][1] is 2.

22. With int[] a = new int[5], reading a[5] causes:

  1. An ArrayIndexOutOfBoundsException at run time
  2. A compile error
  3. The value 0
  4. The value null
Answer

A. An ArrayIndexOutOfBoundsException at run time

Valid indices are 0 to 4; the check happens when the program runs.

23. What is the output?

class Animal {
  String sound() { return "..."; }
}
class Cat extends Animal {
  String sound() { return "Meow"; }
}
public class Main {
  public static void main(String[] args) {
    Animal a = new Cat();
    System.out.println(a.sound());
  }
}
  1. Meow
  2. ...
  3. Cat
  4. Animal
Answer

A. Meow

The overridden method of the actual object (Cat) runs, even through an Animal reference.

24. What is the output?

public class Main {
  static int add(int a, int b) { return a + b; }
  static double add(double a, double b) { return a + b; }
  public static void main(String[] args) {
    System.out.println(add(2, 3) + " " + add(2.0, 3.0));
  }
}
  1. 23 5.0
  2. 5 5
  3. 5 5.0
  4. 5.0 5.0
Answer

C. 5 5.0

Overloading picks the method whose parameter types match the arguments.

25. Method overloading is resolved at:

  1. Run time
  2. Never: Java does not allow overloading
  3. Compile time
  4. Class loading time
Answer

C. Compile time

The compiler chooses the overload from the argument types.

26. Method overriding needs:

  1. A different parameter list
  2. The static keyword
  3. A different method name
  4. The same method name and parameter list in a subclass
Answer

D. The same method name and parameter list in a subclass

An override replaces an inherited method with the same signature.

27. What is the output?

class P {
  static String id() { return "P"; }
}
class C extends P {
  static String id() { return "C"; }
}
public class Main {
  public static void main(String[] args) {
    P p = new C();
    System.out.println(p.id());
  }
}
  1. PC
  2. P
  3. null
  4. C
Answer

B. P

Static methods are not overridden; the reference type (P) decides which one runs.

28. What is the output?

class A {
  String f() { return "A"; }
}
class B extends A {
  String f() { return super.f() + "B"; }
}
public class Main {
  public static void main(String[] args) {
    System.out.println(new B().f());
  }
}
  1. BA
  2. AB
  3. B
  4. A
Answer

B. AB

super.f() calls the parent version, then "B" is added.

29. What is the output?

class A {
  int v = 1;
}
class B extends A {
  int v = 2;
}
public class Main {
  public static void main(String[] args) {
    A o = new B();
    System.out.println(o.v);
  }
}
  1. 2
  2. 1
  3. 3
  4. 0
Answer

B. 1

Fields are not polymorphic: the reference type (A) decides which v is read.

30. Which statement about an abstract class is true?

  1. It cannot have a constructor
  2. It cannot be instantiated with new
  3. It cannot have fields
  4. All its methods must be abstract
Answer

B. It cannot be instantiated with new

An abstract class can have fields, constructors and normal methods, but you cannot create its objects directly.

31. A final method:

  1. Cannot be called
  2. Cannot be overridden
  3. Cannot return a value
  4. Must be static
Answer

B. Cannot be overridden

final on a method stops subclasses from overriding it.

32. What is the output?

interface Greet {
  default String hi() { return "Hello"; }
}
class E implements Greet {}
public class Main {
  public static void main(String[] args) {
    System.out.println(new E().hi());
  }
}
  1. Hello
  2. Greet
  3. null
  4. E
Answer

A. Hello

A default method gives interfaces a body that classes inherit.

33. How many interfaces can one Java class implement?

  1. Only one
  2. None if it already extends a class
  3. Any number
  4. At most two
Answer

C. Any number

A class extends at most one class but can implement many interfaces.

34. Variables declared in an interface are automatically:

  1. public, static and final
  2. protected and abstract
  3. instance variables
  4. private
Answer

A. public, static and final

Interface fields are constants.

35. What is the output?

abstract class S {
  abstract int area();
  int twice() { return 2 * area(); }
}
class R extends S {
  int area() { return 6; }
}
public class Main {
  public static void main(String[] args) {
    System.out.println(new R().twice());
  }
}
  1. 6
  2. 12
  3. 0
  4. 2
Answer

B. 12

twice() in the abstract class calls the subclass's area().

36. What happens if code assigns a new value to a final variable that is already set?

  1. The value changes normally
  2. The assignment is silently ignored
  3. A compile error
  4. Only a run-time exception
Answer

C. A compile error

The compiler rejects any second assignment to a final variable.

37. What is the output?

public class Main {
  static int fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); }
  public static void main(String[] args) {
    System.out.println(fact(5));
  }
}
  1. 5
  2. 15
  3. 120
  4. 24
Answer

C. 120

5! = 5 × 4 × 3 × 2 × 1.

38. What is the output?

public class Main {
  static int fib(int n) { return n < 2 ? n : fib(n - 1) + fib(n - 2); }
  public static void main(String[] args) {
    System.out.println(fib(6));
  }
}
  1. 5
  2. 13
  3. 6
  4. 8
Answer

D. 8

fib: 0, 1, 1, 2, 3, 5, 8, so fib(6) is 8.

39. What is the output?

public class Main {
  static int sumDigits(int n) { return n == 0 ? 0 : n % 10 + sumDigits(n / 10); }
  public static void main(String[] args) {
    System.out.println(sumDigits(4071));
  }
}
  1. 12
  2. 4071
  3. 7
  4. 11
Answer

A. 12

1 + 7 + 0 + 4 = 12.

40. What is the output?

public class Main {
  static void p(int n) {
    if (n == 0) return;
    p(n - 1);
    System.out.print(n);
  }
  public static void main(String[] args) {
    p(3);
  }
}
  1. 3
  2. 321
  3. 123
  4. 0123
Answer

C. 123

The call goes down to p(0) first, and each level prints after its smaller call returns.

41. A recursive method with no base case usually ends with:

  1. A StackOverflowError
  2. A silent infinite loop
  3. A NullPointerException
  4. A compile error
Answer

A. A StackOverflowError

Each call adds a stack frame until the stack runs out.

42. What is the output?

public class Main {
  static int power(int b, int e) { return e == 0 ? 1 : b * power(b, e - 1); }
  public static void main(String[] args) {
    System.out.println(power(2, 10));
  }
}
  1. 1024
  2. 100
  3. 512
  4. 20
Answer

A. 1024

2 multiplied by itself 10 times.

43. What is the output?

public class Main {
  static int count(String s, int i) {
    if (i == s.length()) return 0;
    return (s.charAt(i) == 'a' ? 1 : 0) + count(s, i + 1);
  }
  public static void main(String[] args) {
    System.out.println(count("banana", 0));
  }
}
  1. 6
  2. 2
  3. 1
  4. 3
Answer

D. 3

It counts the letter a in "banana", one position per call.

Mock test

More Technical MCQs topics