refer to the following Date class declaration public class D

refer to the following Date class declaration: public class Date {private int day; private int month; private int year; public Date()//default constructor} ...} public Date(int no, int da, int yr)//constructor {...} public int nonth()//returns month of Date {...} public int day()//returns day of Date {...} public int year()//returns year of Date {...}//Returns String representation of Date as \"n/d/y\". e.g. 4/18/1985. public String toString() {...}} Which of the following will cause an error message? I Date d1 = new Date(8, 2, 1947); Date d2 = d1; II Date d1 = null; Date d2 = d1; III Date d = null; int x = d.year(); I only II only III only II and III only I, IT, and III

Solution

answer is c) III only

Explaination:

I)

Date d1 = new Date(8,2,1947);

Date d2 = d1;

here your are creating any object for class Date and also you are invoking constructor

and as per java code you can assign one object to another object like

Date d2 =d1;

so This is correct.

II)

Date d1 = null;

Date d2 = d1;

as per java coding . null is a type that can be assigned to an object variable.

so you can create object with null.

III)

Date d = null;

int x = d.year();

you have create an null object of d. but null object d return NullPointerException when we call method using it.

exception is Exception in thread \"main\" java.lang.NullPointerException

 refer to the following Date class declaration: public class Date {private int day; private int month; private int year; public Date()//default constructor} ...

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site