Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What is the output of the following code? a . Will the code give any error? If yes, what is the error? Is it a

What is the output of the following code?
a. Will the code give any error? If yes, what is the error? Is it a Compilation error or Runtime error? Why?
b. How can you fix it?
import java.util.Map;
public class Main {
Map namesMap;
public void addName(String name, String course){
namesMap.put(name, course);
}
public Map getNames(){
return namesMap;
}
public static void main(String[] args){
Main m = new Main();
m.addName("Chris", "OOP");
// Hint: there are no issues in the lines below
for (Map.Entry entry : m.getNames().entrySet()){
System.out.println(entry.getKey()+""+ entry.getValue());
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions