Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Which of these is an incorrect Statement?(a) It is necessary to use new operator to initialize an array(b) Array can be initialized using comma separated expressions surrounded by curly braces(c) Array can be initialized when they are declared(d) None of the mentionedI got this question in an interview.My question is based upon Arrays topic in section Data Types, Variables and Arrays of Java

Answer»

Right answer is (a) It is necessary to use new operator to initialize an array

The best I can EXPLAIN: Array can be initialized using both new and COMMA separated expressions surrounded by curly BRACES example : int arr[5] = new int[5]; and int arr[] = { 0, 1, 2, 3, 4};

2.

Which of these is an incorrect array declaration?(a) int arr[] = new int[5](b) int [] arr = new int[5](c) int arr[] = new int[5](d) int arr[] = int [5] newThe question was asked during an interview.My doubt is from Arrays topic in chapter Data Types, Variables and Arrays of Java

Answer» CORRECT choice is (d) int arr[] = int [5] new

Easy explanation: Operator new must be SUCCEEDED by array TYPE and array SIZE.
3.

If an expression contains double, int, float, long, then the whole expression will be promoted into which of these data types?(a) long(b) int(c) double(d) floatI got this question in unit test.The question is from Type Conversions, Promotions and Castings topic in chapter Data Types, Variables and Arrays of Java

Answer»

Correct choice is (C) double

The explanation: If any operand is double the result of an EXPRESSION is double.

4.

Which of these operators is used to allocate memory to array variable in Java?(a) malloc(b) alloc(c) new(d) new mallocThis question was addressed to me at a job interview.The above asked question is from Arrays topic in section Data Types, Variables and Arrays of Java

Answer»

Right choice is (c) new

To elaborate: Operator new allocates a block of MEMORY SPECIFIED by the size of an ARRAY, and gives the reference of memory allocated to the array variable.

5.

Which of these is necessary condition for automatic type conversion in Java?(a) The destination type is smaller than source type(b) The destination type is larger than source type(c) The destination type can be larger or smaller than source type(d) None of the mentionedThe question was posed to me in an interview.Question is taken from Type Conversions, Promotions and Castings in division Data Types, Variables and Arrays of Java

Answer» RIGHT OPTION is (B) The destination type is larger than SOURCE type

To elaborate: None.
6.

Which of these can be returned by the operator &?(a) Integer(b) Boolean(c) Character(d) Integer or BooleanI got this question in exam.My question comes from Literals & Variables topic in division Data Types, Variables and Arrays of Java

Answer»

Right choice is (d) INTEGER or BOOLEAN

Best EXPLANATION: We can use binary ampersand operator on integers/chars (and it returns an integer) or on BOOLEANS (and it returns a boolean).

7.

How to get difference between two dates?(a) long diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();(b) long diffInMilli = java.time.difference(dateTime1, dateTime2).toMillis();(c) Date diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();(d) Time diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();I got this question in quiz.This key question is from Data Type-Date, TimeZone topic in chapter Data Types, Variables and Arrays of Java

Answer»

The correct option is (a) long diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();

For explanation I would SAY: Java 8 provides a method CALLED between which provides Duration between two TIMES.

8.

What does LocalTime represent?(a) Date without time(b) Time without Date(c) Date and Time(d) Date and Time with timezoneI have been asked this question at a job interview.This is a very interesting question from Data Type-Date, TimeZone topic in chapter Data Types, Variables and Arrays of Java

Answer»

Correct answer is (B) Time without DATE

For explanation: LocalTime of joda LIBRARY REPRESENTS time without date.

9.

Which of these is long data type literal?(a) 0x99fffL(b) ABCDEFG(c) 0x99fffa(d) 99671246I have been asked this question in a national level competition.My enquiry is from Literals & Variables topic in section Data Types, Variables and Arrays of Java

Answer» RIGHT choice is (a) 0x99fffL

To explain: DATA type LONG literals are appended by an UPPER or LOWERCASE L. 0x99fffL is hexadecimal long literal.
10.

How is Date stored in database?(a) java.sql.Date(b) java.util.Date(c) java.sql.DateTime(d) java.util.DateTimeThis question was addressed to me at a job interview.My question is based upon Data Type-Date, TimeZone in division Data Types, Variables and Arrays of Java

Answer»

The correct choice is (a) java.sql.DATE

Easy EXPLANATION: java.sql.Date is the DATATYPE of Date STORED in database.

11.

BigDecimal is a part of which package?(a) java.lang(b) java.math(c) java.util(d) java.ioI had been asked this question in class test.The origin of the question is Data Type-BigDecimal topic in chapter Data Types, Variables and Arrays of Java

Answer»

The correct option is (B) java.math

To elaborate: BigDecimal is a part of java.math. This package provides VARIOUS CLASSES for storing NUMBERS and mathematical OPERATIONS.

12.

What is the replacement of joda time library in java 8?(a) java.time (JSR-310)(b) java.date (JSR-310)(c) java.joda(d) java.jodaTimeThis question was addressed to me by my college professor while I was bunking the class.I need to ask this question from Data Type-Date, TimeZone in chapter Data Types, Variables and Arrays of Java

Answer»

The correct CHOICE is (a) java.time (JSR-310)

The explanation: In java 8, we are ASKED to migrate to java.time (JSR-310) which is a core PART of the JDK which REPLACES joda library PROJECT.

13.

Which class does all the Enums extend?(a) Object(b) Enums(c) Enum(d) EnumClassThe question was posed to me in an interview for job.I would like to ask this question from Data Type-Enums in portion Data Types, Variables and Arrays of Java

Answer»

Right choice is (c) ENUM

Easiest EXPLANATION: All ENUMS implicitly extend java.lang.Enum. Since Java does not support multiple INHERITANCE, an enum cannot extend anything else.

14.

Which of the following is not provided by BigDecimal?(a) scale manipulation(b) + operator(c) rounding(d) hashingThis question was posed to me during an internship interview.This key question is from Data Type-BigDecimal topic in chapter Data Types, Variables and Arrays of Java

Answer» RIGHT option is (b) + operator

Best EXPLANATION: toBigInteger() converts BigDecimal to a BIGINTEGER.toBigIntegerExact() converts this BigDecimal to a BigInteger by checking for lost INFORMATION.
15.

Which method returns the elements of Enum class?(a) getEnums()(b) getEnumConstants()(c) getEnumList()(d) getEnum()The question was posed to me during an online exam.This intriguing question comes from Data Type-Enums in division Data Types, Variables and Arrays of Java

Answer»

The correct answer is (b) getEnumConstants()

The EXPLANATION is: getEnumConstants() returns the elements of this enum CLASS or null if this Class OBJECT does not REPRESENT an enum type.

16.

Which of these coding types is used for data type characters in Java?(a) ASCII(b) ISO-LATIN-1(c) UNICODE(d) None of the mentionedI had been asked this question during a job interview.The query is from Character and Boolean Data Types in chapter Data Types, Variables and Arrays of Java

Answer»

Right CHOICE is (c) UNICODE

Explanation: Unicode defines fully international CHARACTER set that can REPRESENT all the CHARACTERS found in all HUMAN languages. Its range is from 0 to 65536.

17.

What is the order of variables in Enum?(a) Ascending order(b) Descending order(c) Random order(d) Depends on the order() methodThe question was posed to me in homework.I'd like to ask this question from Data Type-Enums in portion Data Types, Variables and Arrays of Java

Answer»

The CORRECT ANSWER is (a) Ascending order

Explanation: The COMPARETO() method is implemented to order the variable in ascending order.

18.

Which of these occupy first0 to 127 in Unicode character set used for characters in Java?(a) ASCII(b) ISO-LATIN-1(c) None of the mentioned(d) ASCII and ISO-LATIN1The question was asked in an international level competition.Enquiry is from Character and Boolean Data Types in portion Data Types, Variables and Arrays of Java

Answer»

The CORRECT option is (d) ASCII and ISO-LATIN1

To explain: FIRST 0 to 127 character set in UNICODE are same as those of ISO-LATIN-1 and ASCII.

19.

Which of these values can a boolean variable contain?(a) True & False(b) 0 & 1(c) Any integer value(d) trueThe question was asked during an interview.My doubt stems from Character and Boolean Data Types topic in section Data Types, Variables and Arrays of Java

Answer»

Right option is (a) TRUE & False

To elaborate: BOOLEAN variable can contain only ONE of TWO possible values, true and false.

20.

Which of these literals can be contained in float data type variable?(a) -1.7e+308(b) -3.4e+038(c) +1.7e+308(d) -3.4e+050I had been asked this question in an interview.This key question is from Integer and Floating Data Types in portion Data Types, Variables and Arrays of Java

Answer»

The correct ANSWER is (B) -3.4e+038

Easy explanation: Range of FLOAT data TYPE is -(3.4e38) To +(3.4e38)

21.

An expression involving byte, int, and literal numbers is promoted to which of these?(a) int(b) long(c) byte(d) floatThe question was posed to me in examination.This is a very interesting question from Integer and Floating Data Types topic in section Data Types, Variables and Arrays of Java

Answer»

Right choice is (a) int

For explanation I WOULD SAY: An expression involving BYTES, ints, shorts, literal numbers, the entire expression is PROMOTED to int before any calculation is done.

22.

What is the range of byte data type in Java?(a) -128 to 127(b) -32768 to 32767(c) -2147483648 to 2147483647(d) None of the mentionedI had been asked this question in quiz.Asked question is from Integer and Floating Data Types in portion Data Types, Variables and Arrays of Java

Answer» RIGHT choice is (a) -128 to 127

The best I can explain: BYTE occupies 8 BITS in memory. Its range is from -128 to 127.
23.

What is the range of short data type in Java?(a) -128 to 127(b) -32768 to 32767(c) -2147483648 to 2147483647(d) None of the mentionedThis question was posed to me in my homework.Origin of the question is Integer and Floating Data Types topic in portion Data Types, Variables and Arrays of Java

Answer»

Correct choice is (b) -32768 to 32767

Easy explanation: SHORT OCCUPIES 16 bits in MEMORY. Its RANGE is from -32768 to 32767.