[go: up one dir, main page]

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

500 Java Questions With Answers

Uploaded by

pragatimane1208
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)
433 views2 pages

500 Java Questions With Answers

Uploaded by

pragatimane1208
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
You are on page 1/ 2

500 Java Programming Questions and Answers

Q1. What is a variable in Java and how is it declared? Explain with an example.

A1. A variable in Java is a container that holds data that can be modified during the execution of a

program. Variables are declared with a data type followed by a name. For example:

int age = 25; // 'age' is a variable of type int.

Q2. What is a data type? Explain primitive and reference data types in Java.

A2. A data type defines the type of data a variable can hold. Primitive data types are predefined and

include int, float, char, and boolean. Reference data types include objects and arrays.

Q3. What is the difference between '==' and 'equals()' method in Java?

A3. '==' checks if two object references point to the same memory location, whereas 'equals()'

checks if two objects are logically equivalent (content-wise).

Q4. Write a program to print 'Hello, World!' in Java.

A4. Here?s a simple Java program to print 'Hello, World!':

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!");

Q5. What is type casting in Java? Explain with an example.

A5. Type casting is converting one data type to another. It can be implicit (automatic) or explicit

(manual). For example:

int num = (int) 9.99; // Explicit cast from double to int.

Q6. What is the difference between 'int' and 'Integer' in Java?


A6. 'int' is a primitive data type, whereas 'Integer' is a wrapper class that provides methods to

manipulate integer data.

Q7. What is the purpose of the 'final' keyword in Java?

A7. The 'final' keyword is used to declare constants, prevent method overriding, and prevent

inheritance of classes.

Q8. What is a constructor in Java?

A8. A constructor is a special method used to initialize objects. It is called when an object of a class

is created.

Q9. What is the difference between 'break' and 'continue' statements in Java?

A9. 'break' exits the loop entirely, while 'continue' skips the current iteration and moves to the next

iteration.

Q10. Explain the term 'method overloading' with an example.

A10. Method overloading allows multiple methods with the same name but different parameters.

Example:

void add(int a, int b) {}

void add(double a, double b) {}

You might also like