Search Tutorials


Spring Boot Quiz - MCQ - Multiple Choice Questions | JavaInUse

Spring Boot Quiz - MCQ - Multiple Choice Questions

Q. For the following spring boot code

@SpringBootApplication
public class MyApp {

    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
  
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");
        dataSource.setUsername("root");
        dataSource.setPassword("password");
        return dataSource;
    }
  
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
  
    @Autowired
    private JdbcTemplate jdbcTemplate;
  
    // Rest of the application code...
}
A. The `dataSource` bean will be automatically picked up by Spring Boot and configured as the application's primary `DataSource`.
B. The `jdbcTemplate` bean will be automatically created by Spring Boot using the `dataSource` bean defined in the `dataSource()` method
C. The `dataSource` bean will be created but won't be used by Spring Boot unless explicitly injected into a component.
D. The application will fail to start because there is no `@RestController` or `@Controller` class defined in the code snippet.

Q. What is Spring Initializer primarily used for?

A. Generating boilerplate code for Spring Boot applications
B. Running Spring Boot applications
C. Debugging Spring Boot applications
D. Deploying Spring Boot applications

Q. Which of the following is true about Spring Security?

A. Spring Security is a framework that provides authentication and authorization support for Spring-based applications
B. Spring Security is only applicable for securing web applications and does not support other types of applications
C. Spring Security is a stand-alone framework and cannot be integrated with other Spring modules
D. Spring Security does not provide any out-of-the-box support for implementing multi-factor authentication

Q. Which of the following statements is true about Spring AOP (Aspect-Oriented Programming)?

A. Spring AOP is used for handling exceptions in Spring applications
B. Spring AOP can only be used with Spring MVC applications
C. Spring AOP is primarily used for database access and transaction management
D. Spring AOP allows cross-cutting concerns to be separated from the business logic

Q. Which of the following statements is true about Spring Database integration?

A. Spring provides a built-in database management system for relational databases
B. Spring only supports integration with SQL Server and Oracle databases
C. Spring provides support for accessing and interacting with databases through JDBC and Object-Relational Mapping (ORM) frameworks
D. Spring Database integration is limited to NoSQL databases only

Q. Which of the following is true about Spring Boot?

A. Spring Boot is a separate framework and not a part of the Spring ecosystem
B. Spring Boot requires manual configuration for every aspect of the application
C. Spring Boot simplifies the development of Spring applications by providing an opinionated approach and auto-configuration
D. Spring Boot is only suitable for small-scale applications with limited functionality

Q. Which of the following Spring Boot annotations is used to indicate the main class of a Spring Boot application?

A. @Component
B. @Service
C. @RestController
D. @SpringBootApplication

Q. Which of the following statements is true about the @Autowired annotation in Spring Boot?

A. @Autowired is used to inject dependencies into Spring Boot applications
B. @Autowired is only applicable for injecting dependencies of type String
C. @Autowired is optional and not required for dependency injection in Spring Boot
D. @Autowired is mandatory in Spring Boot and can only be used in service classes

Q. Which of the following code snippets demonstrates the correct configuration of a Spring Boot REST Controller?

A.
@RestController
public class MyController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}
B.
@Controller
@RequestMapping("/api")
public class MyController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}
C.
@RestController
@RequestMapping("/api")
public class MyController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}
D.
@Controller
public class MyController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello() {
        return "Hello, World!";
    }
}





Q. Which of the following code snippets demonstrates the correct configuration of a Spring Boot service class?

A.
@Service
public class MyService {
    private final MyRepository myRepository;

    public MyService(MyRepository myRepository) {
        this.myRepository = myRepository;
    }

    // Service methods here
}
B.
@Component
public class MyService {
    private final MyRepository myRepository;

    @Autowired
    public MyService(MyRepository myRepository) {
        this.myRepository = myRepository;
    }

    // Service methods here
}
C.
@Controller
public class MyService {
    private final MyRepository myRepository;

    public MyService(MyRepository myRepository) {
        this.myRepository = myRepository;
    }

    // Service methods here
}
D.
@Service
@RequestMapping("/service")
public class MyService {
    private final MyRepository myRepository;

    public MyService(MyRepository myRepository) {
        this.myRepository = myRepository;
    }

    // Service methods here
}

Q. Which of the following code snippets demonstrates the correct configuration of a Spring Boot application class?

A.
@Configuration
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
B.
@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
C.
@RestController
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
D.
@Component
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

Q. Which of the following code snippets demonstrates the correct configuration of Spring Boot Security for securing a REST API?

A.
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
                .antMatchers("/api/**").hasRole("ADMIN")
                .anyRequest().permitAll()
                .and()
            .httpBasic();
    }
}
B.
@SpringBootApplication
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/api/**").authenticated()
                .anyRequest().permitAll()
                .and()
            .httpBasic();
    }
}
C.
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/api/**").authenticated()
                .anyRequest().permitAll()
                .and()
            .httpBasic()
                .and()
            .csrf().disable();
    }
}
D.
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/api/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .httpBasic();
    }
}

Q. Which of the following code snippets demonstrates the correct configuration of Spring Boot AOP for logging method execution?

A.
@Component
@Aspect
public class LoggingAspect {
    
    @Pointcut("execution(* com.example.*.*(..))")
    public void myPointcut() {}
    
    @Around("myPointcut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Before method execution");
        Object result = joinPoint.proceed();
        System.out.println("After method execution");
        return result;
    }
    
    // Other advice methods here
}
B.
@Configuration
@Aspect
public class LoggingAspect {
    
    @Pointcut("execution(* com.example.*.*(..))")
    public void myPointcut() {}
    
    @After("myPointcut()")
    public void afterAdvice() {
        System.out.println("After method execution");
    }
    
    // Other advice methods here
}
C.
@Service
@Aspect
public class LoggingAspect {
    
    @Pointcut("execution(* com.example.*.*(..))")
    public void myPointcut() {}
    
    @AfterReturning("myPointcut()")
    public void afterReturningAdvice() {
        System.out.println("After method execution");
    }
    
    // Other advice methods here
}
D.
@Aspect
@Configuration
public class LoggingAspect {
    
    @Pointcut("execution(* com.example.*.*(..))")
    public void myPointcut() {}
    
    @Before("myPointcut()")
    public void beforeAdvice() {
        System.out.println("Before method execution");
    }
    
    // Other advice methods here
}

Q. Which of the following code snippets demonstrates the correct configuration of Spring Boot for connecting to a PostgreSQL database?

A.
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
B.
@Configuration
public class DatabaseConfig {

    @Value("")
    private String url;

    @Value("")
    private String username;

    @Value("")
    private String password;

    @Bean
    public DataSource dataSource() {
        return DataSourceBuilder
                .create()
                .url(url)
                .username(username)
                .password(password)
                .build();
    }
}
C.
@SpringBootApplication
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
D.
@Configuration
@EnableJpaRepositories(basePackages = "com.example.repositories")
public class DatabaseConfig {

    // Other configuration properties and beans here
}

Q. Which of the following code snippets demonstrates the correct configuration of Spring Boot for a batch processing job?

A.
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
B.
@Configuration
@EnableBatchProcessing
public class BatchConfig {

    // Batch job and step configurations here
}
C.
@RestController
public class MyController {

    // Batch job and step configurations here
}
D.
@Configuration
@EnableBatchProcessing
public class BatchConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    // Batch job and step configurations here
}