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

نموذج C

Uploaded by

bnhatm216
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)
27 views

نموذج C

Uploaded by

bnhatm216
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/ 3

‫ مع الحل‬C ‫نموذج‬

1) Explain how Java achieves object-oriented programming. Provide an example.

Java achieves object-oriented programming (OOP) through its fundamental concepts of classes
and objects, which allow developers to model real-world entities and behaviors by:
Classes
Objects
Encapsulation
Inheritance
Polymorphism

Example:
// Class definition
class Car {
String model;

// Constructor
Car(String model) {
this.model = model;
}

// Method
void display() {
System.out.println("Car model: " + model);
}
}

public class OOPExample {


public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car("Toyota");
myCar.display(); // Output: Car model: Toyota
}
}

2) What does the purpose of the ternary operator in Java? provide an example.

The ternary operator (?:) is a shorthand operator for an if-else statement. It makes your code
compact and more readable.

Example:
int age = 18;
String access = (age >= 18) ? "Access granted" : "Access denied";
System.out.println(access);
3) What's the difference between if and switch statements?

4) How does Java handle object-oriented principles like polymorphism?

Java handles object-oriented principles like polymorphism primarily through method overloading
and method overriding.

Example:
class MathOperations {
// Method to add two integers
int add(int a, int b) {
return a + b;
}

// Overloaded method to add three integers


int add(int a, int b, int c) {
return a + b + c;
}

// Overloaded method to add two doubles


double add(double a, double b) {
return a + b;
}
}

public class OverloadingExample {


public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println(math.add(5, 10)); // Output: 15
System.out.println(math.add(5, 10, 15)); // Output: 30
System.out.println(math.add(5.5, 10.5)); // Output: 16.0
}
}
5) Explain the difference between private, and protected access specifiers in Java?

The private specifier restricts access to members only within the same class.
The protected specifier allows access to members within the same package and also in subclasses
(even in different packages)

6) Explain the concept of platform independence in Java.

Platform independence in Java means that Java programs can run on any operating system
without modification. This is achieved through the use of bytecode, which is compiled from Java
source code. When the code is executed, the Java Virtual Machine (JVM) on any platform
interprets the bytecode and translates it into machine-specific instructions.

Example:

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

can be compiled into HelloWorld.class bytecode. This bytecode can run on any system with a
JVM (like Windows, Linux, or macOS) using the command java HelloWorld, producing the
same output on all platforms.

7) Explain the purpose of the import statement in Java and provide an example.

help to narrow down the search performed by the Java compiler by informing it about the classes
and packages used in the class.

Example:

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);

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


String name = input.nextLine();

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

input.close();
}
}

You might also like