Knowee
Questions
Features
Study Tools

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

Question

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The expected result when the above Java code gets compiled and executed is that "True" will be consoled out as a result of execution. This is because an instance of EmployeeDetail is created with a non-null alternateEmail ("[email protected]"), so when we call getAlternateEmail().isPresent(), it returns true.

Similar Questions

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

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();      }}

public class Employee {private int id;private String name;private float salary;private String pancardno;public Employee(){System.out.println("In default constructor");}public Employee(int id, String name, float salary) {this.id = id;this.name = name;this.salary = salary;}public Employee(int id, String name, float salary, String pancardno) {this(id,name,salary); this.pancardno = pancardno; }this();}

You have a class Student:public class Student { public int studentId; public String studentName; public String studentGrade; public Student() { studentId = 1; studentName = null; studentGrade = "1st"; }}You have a child class CollegeStudent:public class CollegeStudent extends Student {}Then you execute the following snippet of code:CollegeStudent myCollegeStudent = new CollegeStudent();System.out.println(myCollegeStudent.studentName);What will be returned by this code?Instruction: Choose the option that best answers the question. Answer ChoicesAn exception will be thrown indicating there is no studentName variable in the CollegeStudent derived classAn exception will be thrown indicating that the studentName variable is not accessible in the derived class CollegeStudentfalsenull

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

1/2

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.