Capgemini Java Interview Questions – Part 1

Published by user on

In this article, we are going to discuss the Capgemini interview questions along with the answers.

Let’s get onto the questions.

Q.1.) What will be printed from following program?

class Vehicle {
	public void accelerate(String speed) {
		System.out.println("Accelerate from String.");
	}
	
	public void accelerate(Object speed) {
		System.out.println("Accelerate from Object.");		
	}
}

public class OverloadingDemo {
	public static void main(String[] args) {
		Vehicle v = new Vehicle();
		v.accelerate(null);
	}
}

a.) Accelerate from String.
b.) Accelerate from Object.
c.) Compiles but fails at runtime
d.) Fails to Compile

Concept: Here the method with most specific data type will be called. As we can see Object is the base class, but String extends from Object class. So String is more specific data type.

Therefore, answer is a.) Accelerate from String.

Q.2.) Can you guess the output of the following program?

		try {
			// some code that throws FileNotFoundException
		} catch (Exception e) {
			// handle exception
		} catch (FileNotFoundException e) {
			// handle file not found exception
		}

a.) Compiler Error
b.) Compiles Correctly

Concept: Handle specific exception first and then handle broader exception. Otherwise, program will result in Compiler Error.

Exception -> Broader Exception
FileNotFoundException -> Specific Exception

Therefore, answer is a.) Compiler Error

Q.3.) What is constructor chaining and what will be the output of below program?

class Vehicle {
	Vehicle() {
		System.out.println("Vehicle's constructor");
	}
}

class Car extends Vehicle {
	Car() {
		System.out.println("Car's constructor");
	}
	
	public void drive() {
		// driving logic here
	}
}

public class ConstructorChaining {
	public static void main(String[] args) {
		Car c = new Car();
		c.drive();
	}
}

a.) Vehicle’s constructor
Car’s constructor

b.) Car’s constructor
Vehicle’s constructor

Concept: Here the concept of constructor chaining is used. When we make a Car object then Car’s constructor gets called, but the first line of Car’s constructor automatically calls the super class i.e Vehicle’s constructor.

Q.4.) In case of below question of dynamic binding what output will be printed?

class Animal {
	public void makeNoise() {
		System.out.println("Animal making noise");
	}
}

class Cat extends Animal {
	public void makeNoise() {
		System.out.println("Cat making noise");
	}
}

public class DynamicBindingDemo {
	public static void main(String[] args) {
		Animal a = new Cat();
		a.makeNoise();
	}
}

a.) Animal making noise
b.) Cat making noise
c.) Fails to compile

Concept: This is a case of dynamic binding. Here the object of class Cat is created and hence Cat’s makeNoise method will be called.

Hence, answer is b.) Cat making noise

Q.5.) What will be printed in case of following Java 8 Program?

public class StreamProgram {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("apple", "banana", "mango");

        stream
            .filter(s -> s.length() > 5).forEach(System.out::println);
    }
}

a.) Fails to compile
b.) banana

Concept: This is self explanatory as the only banana has length greater than 5.

Answer is b.) banana

Q.6.) Is forEach() terminal operation in Java 8?

Yes forEach is terminal operation in Java.

Q.7.) Which is more efficient in following program?

	public static void main(String[] args) {
		StringBuilder sb = new StringBuilder();
		sb.append("a");
		sb.append("b");
		sb.append("b");
		
		String str = new String();
		str.concat("a");
		str.concat("b");
		str.concat("c");
	}

a.) StringBuilder
b.) String

Concept: In case of concatenation StringBuilder is better than String as String creates multiple objects during concatenation.

Answer is a.) StringBuilder

Q.8.) Can we use jetty instead of embedded tomcat in Spring Boot Application?

Yes, by default tomcat is enabled but you can easily replace tomcat with Jetty.