0% found this document useful (0 votes)
1 views

Java_Core_Concepts_with_Programs

The document outlines core Java concepts, including variables, control statements, identifiers, classes, methods, constructors, and garbage collection, along with examples of easy, moderate, and tough questions and programs related to each topic. It provides definitions, differences, and sample code to illustrate the concepts. This serves as a guide for understanding fundamental Java programming principles and commonly asked interview questions.
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)
1 views

Java_Core_Concepts_with_Programs

The document outlines core Java concepts, including variables, control statements, identifiers, classes, methods, constructors, and garbage collection, along with examples of easy, moderate, and tough questions and programs related to each topic. It provides definitions, differences, and sample code to illustrate the concepts. This serves as a guide for understanding fundamental Java programming principles and commonly asked interview questions.
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/ 5

Java Core Concepts with Most Asked Programs

1. Variables and Data Types

Easy:

Q: What is a variable?

A: A variable is a container for storing data values.

Moderate:

Q: Difference between int and float?

A: int stores whole numbers, float stores decimal numbers.

Tough:

Q: Write a program to demonstrate use of different data types.

Program:

class DataTypesDemo {

public static void main(String[] args) {

int a = 10;

float b = 5.5f;

char c = 'A';

boolean d = true;

System.out.println(a + ", " + b + ", " + c + ", " + d);

2. Control Statements

Easy:

Q: What are control statements?

A: They control the flow of execution (if, switch, loops).

Moderate:
Java Core Concepts with Most Asked Programs

Q: Write a program to check if a number is even or odd.

Program:

class EvenOdd {

public static void main(String[] args) {

int num = 7;

if(num % 2 == 0)

System.out.println("Even");

else

System.out.println("Odd");

Tough:

Q: Write a program to print multiplication table of a number.

3. Identifiers, Literals, Keywords

Easy:

Q: Give examples of identifiers.

A: Variable names, method names, class names.

Moderate:

Q: Give examples of literals.

A: 10 (int), 10.5 (float), "Hello" (String), true (boolean).

Tough:

Q: List 5 Java keywords.

A: class, public, static, if, return.

4. Java Class Libraries


Java Core Concepts with Most Asked Programs

Easy:

Q: What is java.lang package?

A: It provides classes like String, Math, System etc.

Moderate:

Q: Use Math library to find square root.

Program:

class SqrtExample {

public static void main(String[] args) {

System.out.println(Math.sqrt(25));

5. Classes and Objects

Easy:

Q: What is an object?

A: Instance of a class.

Moderate:

Q: Write a program with class and object.

Program:

class Student {

String name = "Raj";

void show() { System.out.println(name); }

public static void main(String[] args) {

Student s = new Student();

s.show();

}
Java Core Concepts with Most Asked Programs

6. Methods and Returning Values

Easy:

Q: What is a method?

A: A method is a block of code to perform a task.

Moderate:

Q: Write a method that adds 2 numbers and returns result.

Program:

class Add {

int sum(int a, int b) { return a + b; }

public static void main(String[] args) {

Add obj = new Add();

System.out.println(obj.sum(3, 4));

7. Constructors

Easy:

Q: What is a constructor?

A: Special method used to initialize objects.

Moderate:

Q: Write a program using constructor.

Program:

class Person {

String name;
Java Core Concepts with Most Asked Programs

Person(String n) { name = n; }

void display() { System.out.println(name); }

public static void main(String[] args) {

Person p = new Person("Raj");

p.display();

8. Garbage Collection and finalize()

Easy:

Q: What is garbage collection?

A: Process of reclaiming memory.

Moderate:

Q: What is finalize()?

A: Called before object is destroyed.

Tough:

Program:

class Demo {

protected void finalize() {

System.out.println("Object is garbage collected");

public static void main(String[] args) {

Demo d = new Demo();

d = null;

System.gc();

You might also like