Author: vibssingh
Java – Multiple Choice Questions and Answers – Method, Overloading, Overriding
Welcome to the Java Quiz! This blog post features 25 multiple-choice questions that explore concepts of Methods, Method Overloading and Method Overriding in Java.
1. Which method can be defined only once in a program?
a) main method
b) finalize method
c) static method
d) private method
Answer 1
a) main method
main() method can be defined only once in a program. Program execution begins from the main() method by java runtime system.
2. What is the return type of a method that does not return any value?
a) int
b) float
c) void
d) double
Answer 2
c) void
Return type of a method must be made void if it is not returning any value.
3. What will be the output of the following Java code?
class box
{
int width;
int height;
int length;
int volume;
void volume(int height, int length, int width)
{
volume = width*height*length;
}
}
class Prameterized_method
{
public static void main(String args[])
{
box obj = new box();
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume(3,2,1);
System.out.println(obj.volume);
}
}
Choose one option
a) 0
b) 1
c) 6
d) 25
Answer 3
c) 6
The volume() method calculates the product of the parameters passed to it (3, 2, 1) and assigns it to the object’s volume field. Hence, the output is 3 * 2 * 1 = 6.

4. What will be the output of the following Java program?
class equality
{
int x;
int y;
boolean isequal()
{
return(x == y);
}
}
class Output
{
public static void main(String args[])
{
equality obj = new equality();
obj.x = 5;
obj.y = 5;
System.out.println(obj.isequal());
}
}
Choose one option
a) true
b) Runtime error
c) false
d) 0
Answer 4
a) true
The method isequal() compares x and y using ==. Since both are set to 5, the condition x == y is true, so the method returns true, which gets printed.

5. Which of the following is a method having same name as that of it’s class?
a) finalize
b) delete
c) class
d) constructor
Answer 5
d) constructor
A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.
6. What will be the output of the following Java program?
class overload
{
int x;
int y;
void add(int a)
{
x = a + 1;
}
void add(int a, int b)
{
x = a + 2;
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 0;
obj.add(6);
System.out.println(obj.x);
}
}
Choose one option
a) 5
b) 6
c) 7
d) Runtime error
Answer 6
c) 7
Due to method overloading when the add() method is called with a single argument the value of x is set to argument passed i.e. 6 + 1 is returned. Hence the result is 7.

7. What will be the output of the following Java program?
class overload
{
int x;
int y;
void add(int a)
{
x = a + 1;
}
void add(int a , int b)
{
x = a + 2;
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 0;
obj.add(6, 7);
System.out.println(obj.x);
}
}
Choose one option
a) 6
b) 7
c) 8
d) 9
Answer 7
c) 8
Due to method overloading when the add() method is called with two arguments the value of x is set to the first argument passed i.e. 6 + 2 is returned. Hence the result is 8.

8. What will be the output?
class overload
{
int x;
double y;
void add(int a , int b)
{
x = a + b;
}
void add(double c , double d)
{
y = c + d;
}
overload()
{
this.x = 0;
this.y = 0;
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 2;
double b = 3.2;
obj.add(a, a);
obj.add(b, b);
System.out.println(obj.x + " " + obj.y);
}
}
Choose one option
a) 6 6
b) 6.4 6.4
c) 6.4 6
d) 4 6.4
Answer 8
d) 4 6.4
For obj.add(a,a); ,the function in line number 4 gets executed and value of x is 4. For the next function call, the function in line number 7 gets executed and value of y is 6.4.

9. Which of the following best defines method overloading in Java?
a) Redefining a method with the same name and same parameters in child class.
b) Defining multiple methods with the same name but different parameter lists in the same class.
c) Using a method of the parent class in the child class.
d) Declaring a method as static multiple times.
Answer 9
b) Defining multiple methods with the same name but different parameter lists in the same class.
Overloading occurs when methods have the same name but different parameter lists (number or type).
10. Which of the following is NOT a valid way to overload methods in Java?
a) Different number of parameters.
b) Different types of parameters.
c) Different return types only.
d) Different order of parameters.
Answer 10
c) Different return types only
Overloading cannot be achieved by changing only the return type
11. What will be the output?
class area
{
int width;
int length;
int volume;
area()
{
width=5;
length=6;
}
void volume()
{
volume = width*length*height;
}
}
class cons_method
{
public static void main(String args[])
{
area obj = new area();
obj.volume();
System.out.println(obj.volume);
}
}
Choose one option
a) 0
b) 30
c) 1
d) error
Answer 11
d) error
Variable height is not defined

12. Which of the following allows method overriding?
Choose one option
a) Same method name and parameter list in subclass.
b) Same method name but different parameters in subclass.
c) Same method name but different return type in subclass.
d) Static methods in subclass.
Answer 12
a) Same method name and parameter list in subclass
Overriding requires the same method signature (name + parameters) in superclass and subclass
13. What is the default return type of a method in Java if not specified?
Choose one option
a) int
b) void
c) Object
d) Compilation error
Answer 13
d) Compilation error
Java requires an explicit return type. If omitted, it results in a compilation error.
14. What will be the output of the following code?
class test
{
int a;
int b;
test(int i, int j)
{
a = i;
b = j;
}
void meth(test o)
{
o.a *= 2;
o.b /= 2;
}
}
class Output
{
public static void main(String args[])
{
test obj = new test(10 , 20);
obj.meth(obj);
System.out.println(obj.a + " " + obj.b);
}
}
Choose one option
a) 20 10
b) 10 20
c) 20 40
d) 40 20
Answer 14
a) 20 10
Class objects are always passed by reference, therefore changes done are reflected back on original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b are multiplied and divided by 2 respectively by meth() function of class test. a & b becomes 20 & 10 respectively.

15. What will be the result of this expression?
class Output {
public static int sum(int... x) {
int total = 0;
for (int num : x) {
total += num;
}
return total;
}
public static void main(String args[]) {
System.out.println(sum(10));
System.out.println(sum(10, 20));
System.out.println(sum(10, 20, 30));
System.out.println(sum(10, 20, 30, 40));
}
}
Choose one option
a) only sum(10)
b) only sum(10,20)
c) only sum(10) & sum(10,20)
d) all of the mentioned
Answer 15
d) all of the mentioned
sum is a variable argument method and hence it can take any number as an argument.

16. What is the result of the following code snippet?
class Test
{
public void m1 (int i,float f)
{
System.out.println(" int float method");
}
public void m1(float f,int i)
{
System.out.println("float int method");
}
public static void main(String[]args)
{
Test test =new Test();
test.m1(20,20);
}
}
Choose one option
a) int float method
b) float int method
c) compile time error
d) run time error
Answer 16
c) compile time error
While resolving overloaded method, compiler automatically promotes if exact match is not found. But in this case, which one to promote is an ambiguity.

17. What will be printed?
class Test {
void show(int a) {
System.out.println("int method: " + a);
}
void show(double a) {
System.out.println("double method: " + a);
}
public static void main(String[] args) {
Test obj = new Test();
obj.show(10); // Line 1
obj.show(10.5); // Line 2
obj.show('A'); // Line 3
}
}
Choose one option
a) int method: 10
double method: 10.5
int method: 65
b) int method: 10
double method: 10.5
double method: 65.0
c) Compilation error
d) Runtime error
Answer 17
a) int method: 10
double method: 10.5
int method: 65
obj.show('A') → 'A' is a char (ASCII 65). Char can be promoted to int, so it calls show(int) and prints 65.

18. Which principle of OOP is demonstrated by method overriding?
a) Encapsulation
b) Abstraction
c) Polymorphism
d) Inheritance only
Answer 18
c) Polymorphism
19. If a class has two methods: void add(int a, int b) and int add(int x, int y), what happens?
a) Valid overloading
b) Compile-time error
c) Runtime error
d) Both methods are executed
Answer 19
b) Compile-time error
Same parameter list but different return type only results in compile-time error
20. What is the output of the following code?
class Derived
{
public void getDetails()
{
System.out.printf("Derived class ");
}
}
public class Test extends Derived
{
public void getDetails()
{
System.out.printf("Test class ");
super.getDetails();
}
public static void main(String[] args)
{
Derived obj = new Test();
obj.getDetails();
}
}
Choose one option
a) Test class Derived class
b) Derived class Test class
c) Runtime error
d) Compilation error
Answer 20
a) Test class Derived class
Since getDetails() is overridden in the Test class and super.getDetails() is called inside it, both methods are executed in order.

21. What is the output of this switch statement?
import java.io.IOException;
class Derived {
public void getDetails() throws IOException { // Line 4
System.out.println("Derived class");
}
}
public class Test extends Derived {
public void getDetails() throws Exception { // Line 10
System.out.println("Test class");
}
public static void main(String[] args) throws IOException { // Line 14
Derived obj = new Test();
obj.getDetails();
}
}
a) Compilation error due to line 4
b) Compilation error due to line 10
c) Compilation error due to line 14
d) All the above
Answer 21
b) Compilation error due to line 10
The exception thrown by the overriding method should not be new or more broader checked exception.

22. What will be the output of this while loop?
class Derived
{
protected final void getDetails()
{
System.out.println("Derived class");
}
}
public class Test extends Derived
{
protected final void getDetails()
{
System.out.println("Test class");
}
public static void main(String[] args)
{
Derived obj = new Derived();
obj.getDetails();
}
}
a) Derived class
b) Test class
c) Runtime error
d) Compilation error
Answer 22
d) Compilation error
Final and static methods cannot be overridden.

23. What is the output of the following if-else block?
public class Test
{
public int getData(String temp) throws IOException
{
return 0;
}
public int getData(String temp) throws Exception
{
return 1;
}
public static void main(String[] args)
{
Test obj = new Test();
System.out.println(obj.getData("Happy"));
}
}
a) 0
b) 1
c) Compilation error
d) Runtime error
Answer 23
c) Compilation error
Methods that throws different exceptions are not overloaded as their signature are still the same.

24. Can constructors be overridden in Java?
a) Yes
b) No
c) Only in abstract classes
d) Only for default constructors
Answer 24
b) No
Constructors cannot be inherited, hence cannot be overridden.
25. Which of these statements is correct?
a) Overloading is runtime polymorphism, overriding is compile-time.
b) Overloading is compile-time polymorphism, overriding is runtime.
c) Both overloading and overriding are compile-time polymorphism.
d) Both are runtime polymorphism.
Answer 25
b) Overloading is compile-time polymorphism, overriding is runtime.
We would love to hear from you! Please leave your comments and share your scores in the section below
Java – Multiple Choice Questions and Answers – Basic Java
Last Updated On
Welcome to the Java Quiz! This blog post features 25 multiple-choice questions that explore basic concepts of Java.
1. Which statement is true about Java?
a) Java is a sequence-dependent programming language
b) Java is a code dependent programming language
c) Java is a platform-dependent programming language
d) Java is a platform-independent programming language
Answer 1
d) Java is a platform-independent programming language
2. Which component is used to compile, debug and execute the java programs?
a) JRE
b) JIT
c) JDK
d) JVM
Answer 2
c) JDK
JDK is a core component of Java Environment and provides all the tools, executables and binaries required to compile, debug and execute a Java Program.
3. What will be the output of the following Java code?
class increment {
public static void main(String args[])
{
int g = 3;
System.out.print(++g * 8);
}
}
Choose one option
a) 32
b) 33
c) 24
d) 25
Answer 3
a) 32
Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.

4. What will be the output of the following Java program?
class variable_scope
{
public static void main(String args[])
{
int x;
x = 5;
{
int y = 6;
System.out.print(x + " " + y);
}
System.out.println(x + " " + y);
}
}
Choose one option
a) Compilation error
b) Runtime error
c) 5 6 5 6
d) 5 6 5
Answer 4
a) Compilation error
Second print statement doesn’t have access to y , scope y was limited to the block defined after initialization of x.

5. How do you insert COMMENTS in Java code?
a) /* This is a comment
b) # This is a comment
c) // This is a comment
d) None
Answer 5
c) // This is a comment
6. What will be the output of the following Java program?
public class Test {
public static void main(String[] args) {
int x = 5;
System.out.println(x++);
}
}
Choose one option
a) 5
b) 6
c) Compilation error
d) Runtime error
Answer 6
a) 5
x++ is a post-increment operator, so the current value of x (5) is printed first, and then x is incremented.

7. What will be the output of the following Java program?
public class Test {
public static void main(String[] args) {
String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);
}
}
Choose one option
a) true
b) false
c) Compilation error
d) Runtime error
Answer 7
b) false
== compares references, and s1 and s2 refer to different objects. Use .equals() to compare string contents.

8. What will be the output?
public class Test {
public static void main(String[] args) {
System.out.println(10 + 20 + "30");
}
}
Choose one option
a) 3030
b) 102030
c) 30
d) None
Answer 8
a) 3030
10 + 20 is 30 (integer addition), then 30 + “30” converts 30 to string and concatenates to “30”, resulting in “3030”.

9. Which of the following data types is NOT primitive in Java?
a) int
b) boolean
c) String
d) char
Answer 9
c) String
String is an object, not a primitive type.
10. What is the default value of a boolean variable in Java?
a) true
b) false
c) null
d) 0
Answer 10
b) false
Default value of boolean is false
11. What will be the output?
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a > b ? a : b);
}
}
Choose one option
a) 10
b) 20
c) true
d) false
Answer 11
b) 20
Ternary operator returns a if a > b else returns b.

12. What does the % operator do in Java
Choose one option
a) Division
b) Modulus – gives remainder
c) Multiplication
d) Raises to power
Answer 12
b) Modulus – gives remainder
% returns the remainder after division.
13. Which operator has the highest precedence in Java?
Choose one option
a) +
b) *
c) =
d) ()
Answer 13
d) ()
Parentheses have the highest precedence to control evaluation order.
14. What will be the output of the following code?
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println(a + b * 2);
}
}
Choose one option
a) 30
b) 20
c) 25
d) 15
Answer 14
b) 20
According to operator precedence, multiplication is done before addition: 5 * 2 = 10, then 10 + 10 = 20.

15. What will be the result of this expression?
int x = 10;
int y = 3;
System.out.println(x / y);
Choose one option
a) 3.333
b) 3
c) 4
d) Compilation error
Answer 15
b) 3
Integer division truncates the decimal part. So 10 / 3 = 3.

16. What is the result of the following code snippet?
int a = 5;
System.out.println(a++);
System.out.println(a);
Choose one option
a) 5 5
b) 5 6
c) 6 5
d) 6 6
Answer 16
b) 5 6
a++ prints 5 (post-increment), then a becomes 6.

17. What will be printed?
int x = 4;
System.out.println((x > 3) || (x < 2));
Choose one option
a) true
b) false
c) Compilation error
d) Runtime error
Answer 17
a) true
x > 3 is true, so || (logical OR) returns true without checking the second condition (short-circuiting).

18. Which keyword is used to import a package from the Java API library?
a) import
b) package
c) getlib
d) lib
Answer 18
a) import
19. Which of these is the correct way to declare a method that returns an integer?
a) public void method() {}
b) public int method() { return 1; }
c) public int method() {}
d) public method() { return 1; }
Answer 19
b) public int method() { return 1; }
Method declared to return int must return an int value.
20. What is the output of the following code?
public class Test {
public static void main(String[] args) {
int x = 5;
if (x < 5)
System.out.println("Less");
else if (x == 5)
System.out.println("Equal");
else
System.out.println("More");
}
}
Choose one option
a) Less
b) Equal
c) More
d) Compilation error
Answer 20
b) Equal
x == 5, so the second condition matches and “Equal” is printed.

21. What is the output of this switch statement?
public class Test {
public static void main(String[] args) {
int num = 2;
switch(num + 1) {
case 1: System.out.println("One");
case 2: System.out.println("Two");
case 3: System.out.println("Three");
default: System.out.println("Default");
}
}
}
Choose one option
a) Three
b) Three Default
c) Two Default
d) Two Three Default
Answer 21
b) Three Default
switch(3) matches case 3, no break used, so it also executes default.

22. What will be the output of this while loop?
public class Test {
public static void main(String[] args) {
int i = 1;
while (i <= 3) {
System.out.print(i + " ");
i++;
}
}
}
Choose one option
a) 1 2 3
b) 1 2 3 4
c) Infinite loop
d) 1 2
Answer 22
a) 1 2 3
Loop runs while i <= 3, printing 1 to 3

23. What is the output of the following if-else block?
public class Test {
public static void main(String[] args) {
int x = 4;
if (x % 2 == 0)
if (x % 3 == 0)
System.out.println("Divisible by 6");
else
System.out.println("Divisible by 2 but not by 3");
}
}
Choose one option
a) Divisible by 6
b) Divisible by 3
c) Divisible by 2 but not by 3
d) Compilation error
Answer 23
c) Divisible by 2 but not by 3
4 is divisible by 2 but not by 3

24. Which statement is used to stop a loop?
a) break
b) return
c) exit
d) stop
Answer 24
a) break
25. Which of these are selection statements in Java?
a) break
b) continue
c) for()
d) if()
Answer 25
d) if()
Continue and break are jump statements, and for is a looping statement.
We would love to hear from you! Please leave your comments and share your scores in the section below
SpringBoot – Multiple Choice Questions and Answers – MCQ1
Last Updated On
Welcome to the SpringBoot Quiz! This blog post features 25 multiple-choice questions that explore the concepts of SpringBoot.
1. What is the primary feature of Spring Boot?
a) Simplifies data access
b) Automates code generation
c) Simplifies project setup
d) Enhances UI design
Answer 1
c) Simplifies project setup
Spring Boot simplifies the development of new Spring applications through convention over configuration and automatic setup.
2. Which annotation is used to mark a Spring Boot application entry point?
a) @Configuration
b) @SpringBootApplication
c) @Component
d) @RestController
Answer 2
b) @SpringBootApplication
The @SpringBootApplication annotation is used to mark the main class of a Spring Boot application.
3. The @SpringBootApplication annotation is used to mark the main class of a Spring Boot application.
a) @Component
b) @EnableConfiguration
c) @Configuration
d) @EnableAutoConfiguration
Answer 3
d) @EnableAutoConfiguration
4. What does @SpringBootApplication annotation include?
a) @ComponentScan
b) @EnableAutoConfiguration
c) @SpringBootConfiguration
d) All of the above
Answer 4
d) All of the above
5. Which starter is used for Spring Boot Rest API applications?
a) spring-boot-starter-jdbc
b) spring-boot-starter-web
c) spring-boot-starter-data-jpa
d) spring-boot-starter-security
Answer 5
b) spring-boot-starter-web
The `spring-boot-starter-web` starter includes the necessary dependencies for developing web applications using Spring MVC. It provides essential features for building RESTful APIs, such as HTTP request handling, JSON serialization and deserialization, and web server capabilities.
6. Which starter is used for Spring Boot web applications?
a) spring-boot-starter-jdbc
b) spring-boot-starter-web
c) spring-boot-starter-data-jpa
d) spring-boot-starter-security
Answer 6
b) spring-boot-starter-web
This starter provides the necessary dependencies to develop web applications using Spring MVC.
7. What is auto-configuration in Spring Boot?
a) Automatic setup of the JVM
b) Automatic import of all Java libraries c) Automatic configuration of beans based on classpath settings
d) Automatic update of Spring Boot versions
Answer 7
c) Automatic configuration of beans based on classpath settings
Auto-configuration refers to Spring Boot’s ability to automatically set up application context based on dependencies available in the classpath.
8. What does the following code snippet achieve?
@Bean
public MyService myService() {
return new MyServiceImpl();
}
a) It creates a prototype bean.
b) It registers a singleton bean.
c) It enables caching.
d) It defines a configuration class.
Answer 8
b) It registers a singleton bean.
The @Bean annotation registers a singleton bean in the Spring application context.
9. Which configuration file is commonly used to define properties in a Spring Boot application?
a) application.yml
b) config.properties
c) spring-config.xml
d) web.xml
Answer 9
a) application.yml
The application.yml file is commonly used for defining configuration properties in a Spring Boot application.
10. What is the purpose of the `spring-boot-starter-test` dependency?
a) To start the application faster
b) To provide a built-in testing framework for JUnit, Mockito, etc.
c) To run performance benchmarks
d) To manage database migrations
Answer 10
b) To provide a built-in testing framework for JUnit, Mockito, etc.
This starter provides testing libraries and frameworks for unit tests and mocking, making testing seamless in Spring Boot applications.
11. Which of the following is the correct ?
@RestController
public class ArticleController {
@GetMapping("/articles")
public List<Article> getAllArticles() {
return articleService.findAll();
}
}
Choose one option
a) The method returns a JSON representation of all articles.
b) The method does not return anything.
c) The endpoint is configured for POST requests.
d) The controller does not require a service.
Answer 11
a) The method returns a JSON representation of all articles.
The @GetMapping annotation defines a GET endpoint that returns a JSON representation of all articles.
12. What does the @Value annotation do in the following code?
@Value("${my.api.key}")
private String apiKey;
Choose one option
a) It injects a bean
b) It maps an environment variable
c) It retrieves a property value from application.properties
d) It initializes a method
Answer 12
c) It retrieves a property value from application.properties
The @Value annotation is used to inject a property value from the application.properties file into a field.
13. What will happen if you try to access a bean that is defined with a prototype scope?
@Bean
@Scope("prototype")
public Article article() {
return new Article();
}
Choose one option
a) The same instance is returned every time
b) A new instance is created for each request
c) An exception is thrown
d) The application will not start
Answer 13
b) A new instance is created for each request
A prototype-scoped bean creates a new instance every time it is requested from the application context
14. Which embedded server does Spring Boot use by default for web applications?
Choose one option
a) JBoss
b) WebLogic
c) Tomcat
d) Jetty
Answer 14
c) Tomcat
Spring Boot initially sets Tomcat as its default embedded server, which you can switch to other options like Jetty or Undertow if needed.
15. Which annotation is used to handle exceptions globally in a Spring Boot application?
Choose one option
a) @Controller
b) @RestControllerAdvice
c) @ExceptionHandler
d) @Service
Answer 15
b) @RestControllerAdvice
The @RestControllerAdvice annotation is used to define global exception handling for REST controllers.
16. Given the following configuration, what is the purpose of the @Profile annotation?
@Profile("dev")
@Bean
public DataSource dataSource() {
return new H2DataSource();
}
Choose one option
a) It marks a bean as singleton
b) It specifies that the bean should only be loaded in the ‘dev’ profile
c) It creates a prototype bean
d) It initializes the application context
Answer 16
b) It specifies that the bean should only be loaded in the ‘dev’ profile
The @Profile annotation indicates that the dataSource bean should only be instantiated when the ‘dev’ profile is active.
17. What does the ‘@RestController’ annotation do?
Choose one option
a) Enables automatic logging Configurator
b) Combines @Controller and @ResponseBody annotations
c) Configures a package
d) Sets the application port
Answer 17
b) Combines @Controller and @ResponseBody annotations
The @RestController annotation is used in Spring MVC applications to handle RESTful web services, simplifying the controller implementation.
18. What dependency is required for embedding an H2 database in a Spring Boot application?
a) spring-boot-starter-data-mongo
b) spring-boot-starter-jdbc
c) spring-boot-starter-data-jpa
d) h2
Answer 18
d) h2
The H2 database is an in-memory database, and its dependency needs to be included in the pom.xml or build.gradle file to be used.
19. What is the function of ‘spring-boot-starter-parent’ in a Maven-based Spring Boot project?
a) To hold all project dependencies
b) To provide default project structure
c) To provide default configuration settings and inherit dependency management
d) To define the project version
Answer 19
c) To provide default configuration settings and inherit dependency management
spring-boot-starter-parent provides default configurations and helps manage dependencies, versions, and default plugins for the project.
20. How can you define a scheduled task in a Spring Boot application?
@Scheduled(fixedRate = 2000)
public void reportCurrentTime() {
System.out.println("Current time: " + new Date());
}
Choose one option
a) It runs every 2 seconds
b) It is triggered by an event
c) It executes only once
d) It runs at application startup only
Answer 20
a) It runs every 2 seconds
21. Which of the following layers is not part of a typical Spring Boot application?
a) DAO layer
b) Logger layer
c) Service layer
d) Controller layer
Answer 21
b) Logger layer
Common layers in a Spring Boot application include the DAO, Service, and Controller layers; logging is a cross-cutting concern, not a separate layer.
22. What is the default package used in a Spring Boot project if none is specified?
a) main.java
b) default.package
c) org.springframework.boot
d) The root package specified in the @SpringBootApplication class
Answer 22
d) The root package specified in the @SpringBootApplication class
The @SpringBootApplication annotation triggers auto-configuration only within its package and sub-packages, so it acts as the root package if not specified.
23. How can you disable the default web server in a Spring Boot application? build process?
a) Set server.enabled: false in application.properties
b) Set server.port: 0 in application.properties
c) Use the @DisableWebServer annotation
d) Use a command-line argument to set the server port to 0
Answer 23
b) Set server.port: 0 in application.properties
Setting the server port to 0 effectively disables the embedded web server.
24. How can you switch from using the default Tomcat server to Jetty in a Spring Boot application?
a) Modify server.xml
b) Change the server property in application.properties
c) Exclude Tomcat and include Jetty dependencies in pom.xml
d) Use @EnableJetty annotation
Answer 24
c) Exclude Tomcat and include Jetty dependencies in pom.xml
Tomcat is included by default, so you must exclude it and add Jetty’s dependencies to switch the server.
25. What is Actuator in Spring Boot?
a) A tool to monitor and manage applications
b) A GUI design tool
c) A security framework
d) A configuration tool
Answer 25
a) A tool to monitor and manage applications
Spring Boot Actuator provides various endpoints for monitoring and managing the application, such as health checks and metrics.
We would love to hear from you! Please leave your comments and share your scores in the section below
How to generate an access token and pass to another request in Postman?
Generating and passing an access token in Postman involves a few steps, typically for use with APIs that require authentication.
Implementation Steps
Step 1: Create a Request to Generate the Auth Token
- Open Postman and create a new request (e.g., POST/token).
- Enter the API endpoint that generates the token.
- Go to the Body tab → select raw → choose JSON → add the request body.
- Send the request.
- In the response, you’ll usually get a JSON with the access_token:

Step 2: Save the Token in an Environment Variable
After sending the request, go to the Scripts tab (next to Body/Headers).
Add the below script to capture and store the token:
pm.environment.set("TOKEN", pm.response.json().access_token)

Create a new Environment in Postman (top right → Environments → Add


Save the request.
Step 3: Use the Token in Another Request
- Create a new request.
- Go to the Headers tab.
- Add this header:
Key: Authorization
Value: Bearer {{Token}}

Send the request — Postman will automatically insert the token from the environment variable.

Quick Troubleshooting Checklist
1.Is the token saved correctly in environment variables? Token saved in one environment, but request uses another. Make sure you’ve selected the same environment (top-right dropdown in Postman).
2. Is the Authorization: Bearer {{Token}} header present?
3. Does the account have the correct permissions/scopes? Token is valid, but the user doesn’t have permission for that endpoint. Verify that your account has the right role/permissions.
4. Correct Content-Type is sent. Set header → Content-Type: application/json.
How to set Content-Type in Postman: A Simple Guide
When you’re working with APIs, one of the most important things to understand is the Content-Type header. This header informs the API about the data type you’re sending. This is JSON, XML, or form data. It also specifies what type of response you expect back. If the Content-Type is set incorrectly, your request may fail or the server may not understand the data you’re sending.
Postman is a popular tool for testing APIs. It makes it really easy to add, update, or change headers. This includes Content-Type.
Table Of Contents
What is Content-Type?
The Content-Type header in HTTP requests and responses indicates the data format. It informs the server or client about the format of the data being sent or received. Common content types include:
- application/json: For JSON-formatted data.
- application/xml: For XML-formatted data.
- application/x-www-form-urlencoded: For URL-encoded form data.
- multipart/form-data: For file uploads.
- text/plain: For plain text.
- text/html: Used for sending HTML data
Step by Step Implementation
Create a Collection
Step 1: Create a Collection, click on Collections, and then click on the “+” plus button.

Step 2: Provide a name to the collection – “API Testing”.

Add a request to the Collection
Step 3: To create a new request, click on “Add a request”, if it is a new Collection. Otherwise, click on the 3 dots and select “Add request”.

Step 4: Once you create a new request, then you will get the following window:

Enter the details of request
Step 5: Enter the “name” in the request. Here, the name is “Create User”.
Step 6: Enter the “URL” in the address bar.
https://api.restful-api.dev/objects

Add Headers to the request
Step 7: Enter the “Headers” in the Header tab.
In your Postman request window, navigate to the Headers tab. This tab contains all the HTTP headers that are included in your request.
Scroll through the list of headers to find the Content-Type header. If it is not listed, you can add it manually. Click on the “Key” field and type Content-Type in the field. Then, move to the “Value” field to specify the appropriate Content-Type for your request.

Step 8: Add a Request body to the Post request.
Add the below request in the body.
{
"name": "Apple MacBook Pro 16",
"data": {
"year": 2019,
"price": 1849.99,
"CPU model": "Intel Core i9",
"Hard disk size": "1 TB"
}
}
Press the “Send” button.

Verify the Response
Step 9: Once you press the send button, you will get the response from the server. Make sure you have a proper internet connection; otherwise, you will not get a response.
You can check the status code. Here, we got the status code 200, which means we got a successful response to the request.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
Docker Cheat Sheet 2025
Docker provides the ability to package and run an application in a loosely isolated environment called a container.
This cheatsheet provides a comprehensive and practical reference for common Docker commands. It covers images, containers, volumes, networks, Docker Compose, command combos, and more.
Table of Contents
Docker Basics
Below are the basic commands used for Docker.
1.To get installed docker version
docker --version
2. Log in to Docker Hub or a registry
docker login
3. Log out from a registry
docker logout
4. Start the docker daemon
docker -d
5. Display system-wide information
docker info
Image Management
Docker images are a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings
1.List local images
docker images
2. Build an Image from a Dockerfile
docker build -t [image_name] .
3. Build an Image from a Dockerfile without the cache
docker build -t [image_name] . –no-cache
4. Download an image from Docker Hub/registry
docker pull [image]
5. Tag an image
docker tag [image] [new_image]
6. Delete an Image
docker rmi [image_id]
7. Remove all unused images
docker image prune
Container Management
A container is a runtime instance of a docker image. A container will always run the same, regardless of the infrastructure.
Commands for running, stopping, inspecting, and managing containers.
1.List running containers
docker ps
2. List all containers (including stopped ones)
docker ps -a
3. Run a container from an image
docker run [image]
4. List all containers (including stopped ones)
docker ps -a
5. Run a container from an image
docker run [image]
6. Create and run a container from an image, with a custom name
docker run --name [container_name] [image_name]
7. Run a container in detached mode
docker run -d --name [container_name] [image_name]
8. Assign a name to the container
docker run --name [name] [image]
9. Open a shell inside a running container
docker exec -it [container_name] sh
10. Stop a running container
docker stop [container_name] (or [container-id])
11. Start a stopped container
docker start [container_name] (or [container-id])
12. Restart a container
docker restart [container_name] (or [container-id])
13. Remove a stopped container
docker rm [container_name]
14. View container logs
docker logs [container_name] (or [container-id])
15. To inspect a running container
docker inspect [container_name] (or [container-id])
16. Copy files from container to host
docker cp [container]:/src /dest
Docker Compose
Commands for managing multi-container applications with Docker Compose.
1.Start services defined in docker-compose.yml
docker-compose up
2. Start services defined in docker-compose.yml in detached mode
docker-compose up -d
3. Stop and remove containers, networks, images, and volumes
docker-compose down
4. Build or rebuild services
docker-compose build
5. View output from services
docker-compose logs
6. Run a command in a running service container
docker-compose exec [service] bash
Volume
Commands for creating, inspecting, and mounting Docker volumes.
1.List all volumes
docker volume ls
2. Create a new volume
docker volume create [name]
3. View details of a volume
docker volume inspect [name]
4. Remove a volume
docker volume rm [name]
5. Mount a volume in a container
docker run -v [vol]:/data [image]
Networks
Commands for creating, inspecting, and managing Docker networks.
1.List all networks
docker network ls
2. Create a new network
docker network create [name]
3. View details of a network
docker network inspect [name]
4. Remove a network
docker network rm [name]
5. Connect a container to a network
docker run --network [name] [image]
Git Cheat Sheet
What is Version Control?
Version control systems are tools that manage changes made to files and directories in a project. They enable you to monitor your actions over time. You can revert any changes that you wish to discard. You can also work collaboratively with others on a larger scale. This cheat sheet highlights one of the most widely used systems, Git.
Git configuration
1. Specify the username and email address that will be used with the commits.
1.1. Sets up Git with your name
git config --global user.name "<Your-Full-Name>"
1.2 Sets up Git with your email
git config --global user.email "<your-email-address>"
Starting a project
1. Initializes a new Git repository in the current directory.
git init
2. Creates a local copy of a remote repository.
git clone <repository_url>
3. Clones a specific branch from a remote repository instead of the default branch (main or master)
git clone --branch <branch_name> <repo_url>
Basic Commands
1. Displays the status of your working directory. Options include new, staged, and modified files. It will retrieve branch name, current commit identifier, and changes pending commit.
git status
2. To move files from the Working Directory to the Staging Index
2.1. To add specific files,
git add <file1> <file2> … <fileN>
2.2. To add all the files:
git add .
3. Remove a file from a working directory or staging area
git rm <filename_or_dir>
4. Show changes between working directory and staging area.
git diff [file]
5. Create a new commit from changes added to the staging area with custom message.
git commit -m “[Commit message]”
6. Editing the message of the latest commit
git commit --amend -m "New commit message"
Branch & Merge
1. List all local branches in repository.
git branch
Lists all branches, including: Local branches, Remote-tracking branches (from remote like origin)
git branch -a
2. Create a new branch referencing the current HEAD
git branch [branch-name]
3. Apply commits of the current working branch and apply them to the HEAD of [branch]
git rebase [branch_name]
4.1. Switch working directory to another existing branch
git checkout [branch_name]
4.2. Create and switch to a new branch (older method):
git checkout -b [branch_name]
4.3. Create and switch to a new branch (new method):
git switch -c [branch_name]
5. Remove selected branch, if it is already merged into any other.
git branch -d [branch_name]
6. Force delete a local branch (whether merged or unmerged)
git branch -D [branch_name]
7. Rename the current branch to new branch name
git branch -m [branch_name]
8. Join specified [branch_name] branch into your current branch
git merge [branch_name]
Synchronizing repositories
1.Fetch changes from the remote, but not update tracking branches.
git fetch [remote]
2. Push a local branch named branch to a remote repository (usually named origin)
git push origin branch
3. Fetches changes from the remote repository and merges them into the current branch.
git pull
4. Fetches changes from the remote repository and rebases the current branch onto the updated branch.
git pull --rebase
5. Pushes all branches to the remote repository.
git push --all
6. Lists all remote repositories.
git remote
7. Adds a new remote repository with the specified name and URL.
git remote add [name] [url]
8. Remove a connection to a remote repository.
git remote rm [remote]
9. Rename an existing remote connection.
git remote rename [old_name] [new_name]
Temporary Commits
1. Stashes the changes in the working directory, allowing you to switch to a different branch or commit without committing the changes.
git stash
2. Lists all stashes in the repository.
git stash list
3. Applies and removes the most recent stash from the stash list.
git stash pop
4. Removes the most recent stash from the stash list.
git stash drop
Git Logging
1. Show the commit history for the currently active branch.
git log
2. Shows commit logs for all branches.
git log --all
3. Compares working directory and staging area (unstaged changes).
git diff
4. Shows commits made by a specific author.
git log --author="Name"
5. Shows commits made before a specific date.
git log --until="2024-12-31"
6. Shows the commit history of a specific file.
git log [file]
7. Displays the full details of a specific commit (diff + metadata).
git show
Docker – Advance Level – Multiple Choice Questions and Answers – MCQ1
Last Updated On
Welcome to the Docker Quiz! This blog post features 25 multiple-choice questions that explore advance concepts of Docker.
1. How do Docker containers achieve process isolation on the host system?
a) Virtual Machines
b) Hypervisors
c) Namespaces and cgroups
d) Dedicated resources
Answer 1
c) Namespaces and cgroups
Namespaces are a feature of the Linux kernel that provide isolation for resources such as process IDs, network, user IDs, and file systems.
Docker uses cgroups to allocate resources efficiently among containers running on the same host, preventing one container from consuming excessive resources and impacting others.
2. What is the purpose of a Dockerfile?
a) To create virtual machines
b) To optimize application code
c) To store environment variables
d) To define and build Docker images
Answer 2
d) To define and build Docker images
A Dockerfile contains a set of instructions to define and build a Docker image, specifying dependencies, configuration, and application code.
3. What does the Dockerfile contain?
a) Compiled source code
b) Docker images
c) Binary data
d) Instructions for building a Docker image
Answer 3
Instructions for building a Docker image
A Dockerfile is a text file that contains a set of instructions used to automatically build a Docker image. These instructions define everything needed to assemble the image, such as: Base Image, Package Installation, Execution Commands, File Operations
4. How many types of volumes are there in Docker?
a) 3
b) 2
c) 4
d) 5
Answer 4
b) 2
There are two types of volumes in docker – Named volumes and Bind Mounts
5. Which of the following volume type allows you to share a directory from the host’s filesystem into the container?
a) Named volumes
b) Bind Mounts
Answer 5
b) Bind Mounts
The bind mount volume type allows you to share a directory from the host’s filesystem into the container.
6. In which of the following volume type docker chooses the host location?
a) Named volumes
b) Bind Mounts
Answer 6
a) Named volumes
In the Named volume type docker chooses the host location, whereas you decide the host location in the bind mount type of volume.
7. Which command is used to remove unused Docker objects, such as containers and images?
a) docker prune
b) docker clean
c) docker system prune
d) docker remove
Answer 7
c) docker system prune
8. To list all the networks linked with Docker on the host, the ____ command is used.
a) docker network list
b) docker network ls
c) docker ls
d) network ls
Answer 8
b) docker network ls
To list all the networks linked with Docker on the host, the docker network ls command is used.
9. What is port binding in Docker?
a) Binding environment variables
b) Assigning internal ports to external ones
c) Binding memory resources
d) Binding IP addresses to containers
Answer 9
b) Assigning internal ports to external ones
Port binding in Docker refers to the process of linking internal ports within a Docker container to external ports on the host system. This allows applications running inside the container to communicate with external networks or systems
10. Which of the following is a tool that was built to assist define and distribute multi-container applications?
a) Docker setup
b) Docker compose
c) Docker notify
Answer 10
b) Docker compose
Docker Compose is a tool that was built to assist define and distribute multi-container applications.
11. Which of the following command is used to display the statistics of a running container?
Choose one option
a) Docker statistics
b) Stats
c) Docker statics
d) Docker stats
Answer 11
d) Docker stats
The stats command is used to display the statistics of a running container.
12. Which command is used to view the logs of a running container?
Choose one option
a) docker view [container ID]
b) docker logs [container ID]
c) docker inspect [container ID]
d) docker output [container ID]
Answer 12
b) docker logs [container ID]
13. What is the default Docker network mode?
Choose one option
a) host
b) none
c) bridge
d) overlay
Answer 13
c) bridge
14. After making changes to a running container, how can you save those changes into an image?
Choose one option
a) docker save
b) docker commit
c) docker store
d) docker snapshot
Answer 14
b) docker commit
15. What does the `ENTRYPOINT` directive do in a Dockerfile?
a) Sets an environment variable
b) Specifies the command that will run on container start
c) Exposes a port
d) Confirms the image build
Answer 15
b) Specifies the command that will run on container start
16. What is the function of the `Dockerfile` directive `COPY`?
Choose one option
a) Copies files and directories from localhost to the image
b) Moves data between volumes
c) Copies data between containers
d) Duplicates Docker images
Answer 16
a) Copies files and directories from localhost to the image
17. What is the syntax used to specify base image in a Dockerfile?
Choose one option
a) FROM [image name]
b) BASE [image name]
c) SOURCE [image name]
d) INITIAL [image name]
Answer 17
a) FROM [image name]
18. What is the primary function of the .dockerignore file?
Choose one option
a) To list all images to be pulled from the Docker Hub
b) To specify commands to run inside a container
c) To prevent certain files and directories from being copied into an image
d) To provide metadata about a Docker image
Answer 18
c) To prevent certain files and directories from being copied into an image
The .dockerignore file allows users to exclude files and directories from being copied to the image during the build process, much like .gitignore does for git repositories.
19. In a docker-compose.yml file, what is the function of the depends_on key?
a) Specifies the base images for services
b) Specifies the build context for services
c) Specifies the order in which services are started
d) Specifies the network links between services
Answer 19
c) Specifies the order in which services are started
In a docker-compose.yml file, the depends_on key indicates the order in which services should be started. A service with a depends_on key will not start until the services it depends on have been started.
20. What is the primary purpose of Docker Swarm?
a) Image version management
b) Multi-host container orchestration
c) Container storage optimization
d) Automated container build pipeline
Answer 20
b) Multi-host container orchestration
Docker Swarm is a native clustering and orchestration tool for Docker. It allows you to create and manage a swarm of Docker nodes and orchestrate services across multiple hosts.
21. What command initializes a node as a Docker Swarm manager?
a) docker swarm init
b) docker swarm start
c) docker swarm create
d) docker swarm manager
Answer 21
a) docker swarm init
The docker swarm init command initializes the current node as a Docker Swarm manager, which manages the infrastructure of a swarm.
22. How can you inspect the details of a Docker network?
a) docker network view NETWORK_NAME
b) docker network show NETWORK_NAME
c) docker network detail NETWORK_NAME
d) docker network inspect NETWORK_NAME
Answer 22
d) docker network inspect NETWORK_NAME
The docker network inspect command is used to display detailed information about a network.
23. Which command creates a Docker volume?
a) docker storage create
b) docker add volume
c) docker volume create
d) docker create
Answer 23
c) docker volume create
The docker volume create command creates a new Docker volume for persistent storage.
24. Why is Docker commonly integrated with CI/CD pipelines?
a) Consistent environments across testing and production
b) Easier dependency management
c) Faster build and deployment times
d) All of the mentioned
Answer 24
d) All of the mentioned
Docker integration with CI/CD ensures faster deployments, dependency management, and environment consistency.
25. Why is Kubernetes often paired with Docker?
a) For debugging local machines
b) For virtualizing operating systems
c) For orchestrating large-scale container deployments
d) For building individual containers
Answer 25
c) For orchestrating large-scale container deployments
Kubernetes orchestrates, deploys, and manages containers at scale, complementing Docker’s container creation capabilities.
We would love to hear from you! Please leave your comments and share your scores in the section below
Docker Tutorials
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure. Docker packages software into standardized units called containers. These containers have everything the software needs to run. They include libraries, system tools, code, and runtime.

| Chapter 1 How to install Desktop Docker on Windows 11 |
| Chapter 2 Docker – Basic Level – Multiple Choice Questions and Answers – MCQ1 |
| Chapter 3 Docker – Advance Level – Multiple Choice Questions and Answers – NEW |
| Chapter 4 Docker Cheat Sheet 2025 – NEW |