Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Kunal Java Unit-1

Uploaded by

manglamdubey2011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Kunal Java Unit-1

Uploaded by

manglamdubey2011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Index:

1. Write down the basic concept of object oriented programming.

2. Difference bw oop and pop.

3. Explain selection statements (if, switch) and iteration statements (while,

do-while, for) with suitable examples.

4. Explain role of class and object in Java.

5. How to read console input in Java?

6. What is constructor overloading?

7. What are the keywords in java? Explain final this and static keyword.
1. Write down the basic concept of object oriented programming.

ANS-

1. Objects:

• Objects represent real-world entities and consist of both data (attributes/properties) and
behaviors (methods/functions).

2. Classes:

• Classes serve as blueprints or templates for creating objects. They define the structure
and behavior of objects.
3. Encapsulation:

• Encapsulation protects the valuable items (data) by hiding them within the chest and only
allowing access through the designated keys (methods), ensuring security and integrity.

4. Abstraction:

• Abstraction focuses on hiding complex implementation details and showing only the
necessary features of an object. It allows the creation of simpler and more manageable
models.
5. Inheritance:

• Inheritance allows a class (subclass/derived class) to inherit properties and behaviors


from another class (superclass/base class). It promotes code reusability and the creation
of a hierarchical structure among classes.

6. Polymorphism:

• polymorphism allows different objects to be treated as the same type, enabling flexibility
and uniform interaction with methods or functions.
2. Difference bw oop and pop.

ANS-

Feature Procedural Programming Object-Oriented Programming


(POP) (OOP)

Focus Steps to solve a problem Objects and their interactions

Data and Separate Combined into objects


Functions

Code Structure List of steps Objects and their relationships

Data Protection Data can be accessed freely Data is protected within objects

Reusing Code Harder to reuse Easier to reuse through inheritance

Managing Can be difficult Easier to manage due to objects


Changes

Growing Projects Becomes complex Can handle larger projects better

POP eg: OOP eg:


#include <stdio.h>
public class HelloWorld {

public static void main(String[] args) {


int main() {
System.out.println("Hello, World!");
printf("Hello, World!\n");
}
return 0;
}
}
3. Explain selection statements (if, switch) and iteration statements (while, do-while, for)
with suitable examples.

ANS-

1. Selection Statements:

• if Statement: Executes a block of code if a specified condition is true. For example:

if (age > 18) {

System.out.println("Adult");

• switch Statement: Selects one of many code blocks to execute based on a variable’s value.
For example:

switch (day) {

case 1: System.out.println("Monday"); break;

case 2: System.out.println("Tuesday"); break;

// other cases

2. Iteration Statements:

• while Loop: Repeats a block of code while a condition is true. For example:

while (i < 5) {

System.out.println(i);

i++;

• do-while Loop: Similar to while, but the code block is executed at least once. For example:

do {

System.out.println(i);

i++;

} while (i < 5);


• for Loop: Used for iterating over a range of values. For example:

for (int i = 0; i < 5; i++) {

System.out.println(i);

4. Explain role of class and object in Java.

ANS-

1. Classes:

• Definition: A class is a blueprint for creating objects. It defines attributes (fields) and
methods (functions) that objects created from the class can use. For example:

public class Person {

String name;

int age;

void greet() {

System.out.println("Hello, my name is " + name);

2. Objects:

• Definition: An object is an instance of a class. It contains actual values and can use the
methods defined in the class. For example:

Person p = new Person();

p.name = "Alice";

p.age = 30;

p.greet();
5. How to read console input in Java?

ANS- For it we use scanner classes and follows the following structure in our source
programme:

• Import: import java.util.Scanner;

• Create Scanner Object: Scanner scanner = new Scanner(System.in);

Source programme:

import java.util.Scanner;

public class InputExample {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your name:");

String name = scanner.nextLine();

System.out.println("Hello, " + name);

}
6. What is constructor overloading?

ANS-

Constructor Overloading allows multiple constructors in a class.

Constructors differ by the number and type of parameters.

Benefits: Provides multiple ways to initialize objects.

public class Rectangle {

int length, width;

// Constructor with no parameters

Rectangle() {

length = 1;

width = 1;

// Constructor with parameters

Rectangle(int l, int w) {

length = l;

width = w;

}
7. What are the keywords in java? Explain final this and static keyword.

ANS- In Java, keywords are reserved words that have specific meanings and purposes within
the language. They cannot be used as identifiers (e.g., variable names, function names) in your
code.

Final

It's used to create things that can't be altered after they're made.

• Final variable: A value that can't be changed once it's set. It's like a constant.

• Final method: A method that can't be modified by other classes.

• Final class: A class that can't be inherited by other classes.

public class Circle {

private final double PI = 3.14159; // Final variable

private final int radius; // Final variable initialized in constructor

public Circle(int radius) {

this.radius = radius; // Using 'this' to refer to the instance variable

public double calculateArea() {

return PI * radius * radius;

This

It refers to the current object you're working with.

• It helps to differentiate between variables inside and outside an object.

• It's used to call other constructors within the same class.


public class Car {

private String color;

public Car(String color) {

this.color = color; // Using 'this' to assign the parameter to the instance variable

public void setColor(String color) {

this.color = color; // Using 'this' to refer to the instance variable

Static

Imagine a shared room. Everyone can access things in the room without owning the room.
That's static. It belongs to the class itself, not to specific objects.

• Static variable: A variable that belongs to the class, not an object. All objects share the
same value.

• Static method: A method that can be called without creating an object.

public class MathUtils {

public static final double PI = 3.14159; // Static final variable

public static double calculateArea(double radius) {

return PI * radius * radius;

You might also like