0% found this document useful (0 votes)
13 views

Spring Framework MCQs With Code Snippets

The document contains multiple-choice questions (MCQs) related to the Spring Framework, each accompanied by code snippets and their expected outputs or functionalities. It covers various aspects of Spring, including controllers, services, repositories, and configuration classes. Each question provides options with the correct answer indicated for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Spring Framework MCQs With Code Snippets

The document contains multiple-choice questions (MCQs) related to the Spring Framework, each accompanied by code snippets and their expected outputs or functionalities. It covers various aspects of Spring, including controllers, services, repositories, and configuration classes. Each question provides options with the correct answer indicated for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Spring Framework MCQs with Code Snippets

1. What will the following code output?

java

VerifyOpen In EditorRunCopy code

1@RestController

2public class HelloController {

3 @GetMapping("/hello")

4 public String sayHello() {

5 return "Hello, World!";

6 }

7}

A) Hello, World!
B) /hello
C) Error
D) None of the above
Answer: A

2. What does the following code do?

java

VerifyOpen In EditorRunCopy code

1@Service

2public class UserService {

3 public User getUser () {

4 return new User("John");

5 }

6}

A) Defines a controller
B) Defines a service class
C) Defines a repository
D) None of the above
Answer: B

3. What will happen if you run the following code?

java

VerifyOpen In EditorRunCopy code

1@Autowired
2private UserService userService;

4public void printUser () {

5 System.out.println(userService.getUser ().getName());

6}

A) Prints the user's name


B) Throws a NullPointerException
C) Prints "null"
D) Does nothing
Answer: A (if userService is properly injected)

4. What does the following code represent?

java

VerifyOpen In EditorRunCopy code

1@Configuration

2public class AppConfig {

3 @Bean

4 public UserService userService() {

5 return new UserService();

6 }

7}

A) Defines a Spring configuration class


B) Defines a controller
C) Defines a service
D) None of the above
Answer: A

5. What will the following code output?

java

VerifyOpen In EditorRunCopy code

1@RestController

2public class MathController {

3 @GetMapping("/add")

4 public int add(@RequestParam int a, @RequestParam int b) {

5 return a + b;

6 }
7}

A) Returns the sum of a and b


B) Returns an error
C) Returns a string
D) None of the above
Answer: A

6. What does the following code do?

java

VerifyOpen In EditorRunCopy code

1@Entity

2public class User {

3 @Id

4 @GeneratedValue(strategy = GenerationType.IDENTITY)

5 private Long id;

6 private String name;

7}

A) Defines a Spring service


B) Defines a JPA entity
C) Defines a controller
D) None of the above
Answer: B

7. What will the following code output?

java

VerifyOpen In EditorRunCopy code

1@Component

2public class MyComponent {

3 public void doSomething() {

4 System.out.println("Doing something!");

5 }

6}

A) Does nothing
B) Prints "Doing something!"
C) Throws an error
D) None of the above
Answer: B
8. What does the following code do?

java

VerifyOpen In EditorRunCopy code

1@Repository

2public class UserRepository {

3 public User findById(Long id) {

4 // logic to find user

5 }

6}

A) Defines a service
B) Defines a repository
C) Defines a controller
D) None of the above
Answer: B

9. What will the following code output?

java

VerifyOpen In EditorRunCopy code

1@RestController

2public class GreetingController {

3 @GetMapping("/greet")

4 public ResponseEntity<String> greet() {

5 return ResponseEntity.ok("Greetings!");

6 }

7}

A) Returns "Greetings!" with status 200


B) Returns an error
C) Returns "Greetings!" with status 404
D) None of the above
Answer: A

10. What does the following code do?

java

VerifyOpen In EditorRunCopy code

1@Component

2public class MyService {


3 @Autowired

4 private MyComponent myComponent;

6 public void execute() {

7 myComponent.doSomething();

8 }

9}

A) Calls the doSomething method of MyComponent


B) Throws an error
C) Does nothing
D) None of the above
Answer: A

11. What will the following code output?

java

VerifyOpen In EditorRunCopy code

1@Controller

2public class HomeController {

3 @GetMapping("/")

4 public String home() {

5 return "home";

6 }

7}

A) Returns the view named "home"


B) Returns an error
C) Returns "home" as a string
D) None of the above

You might also like