mport java.util.Optional;@SuppressWarnings("")public class EmployeeDetail { private String alternateEmail; public Optional<String> getAlternateEmail() { return Optional.ofNullable(alternateEmail); } public void setAlternateEmail(String alternateEmail) { this.alternateEmail=alternateEmail; }}public class TechSol { public static void main(String[] args) { EmployeeDetail employee1=new EmployeeDetail(); EmployeeDetail employee2=new EmployeeDetail(); employee1.setAlternateEmail("[email protected]"); System.out.println(employee1.getAlternateEmail().orElse("[email protected]")); System.out.println(employee2.getAlternateEmail().orElse("[email protected]")); }}What can be expected when the above code is compiled and executed? [email protected] will be printed [email protected] and [email protected] will be [email protected] and null will be printedNullPointerException will be raised after printing [email protected] by Infosys Wingspan
Question
mport java.util.Optional;@SuppressWarnings("")public class EmployeeDetail { private String alternateEmail; public Optional<String> getAlternateEmail() { return Optional.ofNullable(alternateEmail); } public void setAlternateEmail(String alternateEmail) { this.alternateEmail=alternateEmail; }}public class TechSol { public static void main(String[] args) { EmployeeDetail employee1=new EmployeeDetail(); EmployeeDetail employee2=new EmployeeDetail(); employee1.setAlternateEmail("[email protected]"); System.out.println(employee1.getAlternateEmail().orElse("[email protected]")); System.out.println(employee2.getAlternateEmail().orElse("[email protected]")); }}What can be expected when the above code is compiled and executed? [email protected] will be printed [email protected] and [email protected] will be [email protected] and null will be printedNullPointerException will be raised after printing [email protected] by Infosys Wingspan
Solution
The expected output when the above code is compiled and executed is "[email protected]" and "[email protected]" will be printed.
Here's the step by step explanation:
- Two EmployeeDetail objects, employee1 and employee2, are created.
- The alternateEmail of employee1 is set to "[email protected]".
- When we try to get the alternateEmail of employee1, it is present, so "[email protected]" is printed.
- For employee2, no alternateEmail has been set, so the Optional is empty.
- When we try to get the alternateEmail of employee2, since it is not present, the orElse method returns the default value "[email protected]", which is then printed.
Similar Questions
import java.util.Optional;public class EmployeeDetail { private String alternateEmail; public Optional<String> getAlternateEmail() { return Optional.ofNullable(alternateEmail); } public EmployeeDetail(String alternateEmail) { this.alternateEmail=alternateEmail; }}public class TechSol { public static void main(String[] args) { EmployeeDetail employeeDetailOptional = new EmployeeDetail(new String("[email protected]")); System.out.println(employeeDetailOptional.getAlternateEmail().isPresent()); }}What can be expected when the above code gets compiled and executed? True will be consoled out as a result of executionFalse will be consoled out as a result of executionCompilation will fail because alternateEmail needs to get initialized when declaredExecution will fail because alternateEmail needs to get initialized when declared
mport java.util.*;class Queue1 { public static void main(String args[]) { PriorityQueue<String> queue = new PriorityQueue<String>(); queue.add("Amit"); queue.add("Vijay"); queue.add("Karan"); System.out.println("Printing queue elements:"); Iterator itr = queue.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } queue.remove(); System.out.println("Printing queue elements:"); Iterator<String> itr2 = queue.iterator(); while (itr2.hasNext()) { System.out.println(itr2.next()); } }}Select one:a.Printing queue elements:AmitVijayKaranPrinting queue elements:Karanb.Printing queue elements:AmitVijayKaranPrinting queue elements:KaranVijayc.Printing queue elements:AmitVijayKaranPrinting queue elements:AmitVijayd.Printing queue elements:AmitVijayKaranPrinting queue elements:Amit
Arrange the code shuffle provided such that - first you provide the no argument constructor that prints "In default constructor" , - next is the constructor with id, name and salary as arguments that invokes the no arg constructor and sets the attributes passed as attribute and - finally the constructor with id, name, salary and pancardno as arguments that invokes the 3 argument constructor and then sets the pancardno.public class Employee {public Employee(){private int id;private String name;private float salary;private String pancardno;}this();public Employee(int id, String name, float salary) {this.id = id;this.name = name;this.salary = salary;} this.pancardno = pancardno; }this(id,name,salary);System.out.println("In default constructor");}public Employee(int id, String name, float salary, String pancardno) {
public class Employee { public int id; public String name; private int sal; public Employee(int id, String name , int sal) { this.id = id; this.name = name; this.sal = sal; } public void setSal(int sal) { this.sal = sal; } public int getSal() { return sal; } public static List<Employee> getEmpList() { List<Employee> list = new ArrayList<>(); list.add(new Employee(111, "A", 2000)); list.add(new Employee(222, "B", 3000)); list.add(new Employee(333, "C", 4000)); list.add(new Employee(444, "D", 5000)); return list; }}public class Appp { public static void main(String[] args) { List<Employee> list = Employee.getEmpList(); for(Employee e : list) { //line 1 e.setSal(e.getSal()*2); //line 2 } //line 3 }}Choose the appropriate option to replace code line 1 to 3 to get the same output from the program. listEmployees.forEach(e -> e.setSal(e.getSal()*2));for(Employee e : listEmployees) {e -> e.setSal(e.getSal()*2);}for(Employee e : listEmployees) {forEach(e -> e.setSal(e.getSal()*2));}for(Employee e : listEmployees) {e.forEach() -> e.setSal(e.getSal()*2);}SubmitPowered by Infosys Wingspan
You are provided with the sample code for the class Employee.Create a constructor for the class Employee, so that, when an object is created for the class Employee, it should print the message as "In Default constructor".public class Employee{ Answer { System.out.println("In Default constructor"); } public static void main(String args[]) { Employee empObj = new Employee(); }}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.