0% found this document useful (0 votes)
18 views2 pages

Java Objective Questions & Answers Guide

The document contains a series of objective type questions and answers related to Java programming, covering topics such as variables, data types, operators, control statements, arrays, strings, object-oriented programming, exception handling, and miscellaneous concepts. Each question is followed by multiple-choice answers, with the correct answer indicated. It serves as a study guide for Java coding knowledge.

Uploaded by

rudramilke878
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Java Objective Questions & Answers Guide

The document contains a series of objective type questions and answers related to Java programming, covering topics such as variables, data types, operators, control statements, arrays, strings, object-oriented programming, exception handling, and miscellaneous concepts. Each question is followed by multiple-choice answers, with the correct answer indicated. It serves as a study guide for Java coding knowledge.

Uploaded by

rudramilke878
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Coding Objective Type Questions with

Answers

1. Variables & Data Types

1. What will be the output of the following code?


int x = 5;
int y = 2;
[Link](x / y);
a) 2.5 b) 2 c) 3 d) Error
Answer: b) 2

2. Which of the following is the size of a long in Java?


a) 4 bytes b) 8 bytes c) 2 bytes d) 16 bytes
Answer: b) 8 bytes

2. Operators

3. What will be the output?


int a = 10;
[Link](++a + a++);
a) 21 b) 22 c) 20 d) 23
Answer: b) 22

4. Which operator is used for short-circuit AND in Java?


a) & b) && c) | d) ||
Answer: b) &&

3. Control Statements

5. What will be the output?


for(int i=1; i<=5; i++) {
if(i==3) break;
[Link](i);
}
a) 12345 b) 12 c) 123 d) 1245
Answer: b) 12

6. Which statement is used to skip the current iteration in a loop?


a) exit b) return c) continue d) break
Answer: c) continue

4. Arrays & Strings

7. What will be the output?


String s1 = "Java";
String s2 = new String("Java");
[Link](s1 == s2);
a) true b) false c) Error d) Depends on compiler
Answer: b) false

8. Which method is used to get the length of an array?


a) size() b) length c) length() d) getSize()
Answer: b) length

5. OOP (Classes & Objects)

9. Which concept of OOP refers to reusing code by creating a new class from an existing one?
a) Encapsulation b) Inheritance c) Polymorphism d) Abstraction
Answer: b) Inheritance

10. Which keyword is used to prevent inheritance of a class?


a) abstract b) final c) static d) private
Answer: b) final

6. Exception Handling

11. Which of the following is the superclass of all exceptions in Java?


a) Throwable b) Error c) Exception d) RuntimeException
Answer: a) Throwable

12. What will happen if an exception is not caught in Java?


a) The program ignores it
b) The program will crash
c) It automatically recovers
d) It is converted into a warning
Answer: b) The program will crash

7. Miscellaneous

13. Which of these is not a Java keyword?


a) static b) void c) main d) final
Answer: c) main

14. Which of the following is true about main method in Java?


a) It must return int
b) It must be static
c) It must be private
d) It must be final
Answer: b) It must be static

15. Which method is called automatically when an object is created?


a) finalize() b) constructor c) destructor d) new()
Answer: b) constructor

Common questions

Powered by AI

The 'break' statement terminates the closest enclosing loop prematurely when encountered. In the given loop, 'break' is triggered when i equals 3, causing the loop to stop executing, resulting in an output of 12 as only numbers 1 and 2 are printed before the loop exits .

An uncaught exception terminates the program's normal flow, leading to a crash because Java has no mechanism to handle the exception, leaving the program in an undefined state. This can be prevented by using try-catch blocks to handle exceptions or propagating exceptions using the 'throws' keyword to higher levels where they can be managed effectively .

This code prints false because '==' compares object references, not the content they point to. 's1' is interned in the string pool, while 's2' is a new object with the same content, but a different reference. This illustrates the concept of reference equality versus object equality in Java .

The expression 'System.out.println(++a + a++)' involves both pre-increment and post-increment operations. In this case, '++a' increments a to 11 before it is used, and 'a++' uses 11 but increments a afterwards. Thus, the expression evaluates to 11 + 11 with a final value of 12 after both operations, resulting in the printed output of 22 .

'Throwable' is the superclass of all errors and exceptions in Java, forming the root class of the exception hierarchy. It allows error handling mechanisms to catch and handle different errors and exceptions in a uniform way .

A 'static' method belongs to the class, not instances, allowing it to be called on the class itself without creating an object. The 'main' method in Java must be static so it can be called by the Java runtime without an object. A 'final' method cannot be overridden in subclasses, ensuring the method's implementation remains consistent across inheritance chains. The 'main' method cannot be marked final as it would not serve any specific purpose given its static nature and typical usage .

A 'constructor' method is automatically called when an object is created in Java. It initializes the newly created object and can set initial values for object attributes or perform setup procedures necessary at the time of object creation .

The 'continue' statement skips the current iteration of a loop, proceeding with the next iteration without terminating the loop entirely. Unlike 'break', which exits the loop completely when encountered, 'continue' allows the loop to continue running, just bypassing the current cycle of execution .

In Java, dividing two integers uses integer division, which truncates the decimal part and only returns the quotient. Therefore, when 'System.out.println(x / y)' is called with x = 5 and y = 2, the result is 2, not 2.5, because integer division discards any remainder .

Inheritance in OOP allows a new class to acquire the properties and methods of an existing class, promoting code reuse and hierarchical classification. In Java, the keyword 'final' is used to prevent a class from being extended, thereby inhibiting inheritance .

You might also like