Cat 1
Cat 1
Paper 1 - Question 1
a) Briefly explain the meaning of the following terms as used in java programming. Give a
java code example for each case.
i) Class - A class is a template or blueprint for creating objects. It defines state (variables) and
behavior (methods) that an object will have.
ii) Variable - A variable is a container for data used to store values during program execution.
iii) Syntax Error - A syntax error occurs when code does not follow the rules of the Java
language, such as missing semicolons or incorrect syntax.
public class Syntax {
public static void main(String[] args) {
System.out.println("Hello, world!")
}
}
The above code is missing a semicolon as its syntax error.
- Platform Independence: Java bytecode can run on any platform with a JVM (Java
Virtual Machine).
- Object-Oriented Language: Everything in Java revolves around objects and classes,
promoting code reuse and modular design.
- Security: Java provides built-in security features like bytecode verification and a security
manager.
- Rich API: Java comes with an extensive set of APIs for handling I/O, networking, and
data structures.
- Robust IDEs: Development tools like Eclipse and IntelliJ make it easier to develop, test,
and debug Java code.
- Omnipresent: Java is widely used across platforms, from web development to mobile
apps and backend services.
d) Write a simple Java program to explain the working for do...while loop, write the
expected output of your program.
e) With a well written Java Program explain the difference between method overloading
and method overriding [6 Marks]
Method overloading occurs when multiple methods in the same class have the same name but
different parameters
class Calculator {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int sum1 = calculator.add(5, 10);
System.out.println("Sum of two integers: " + sum1);
int sum2 = calculator.add(5, 10, 15);
System.out.println("Sum of three integers: " + sum2);
}
}
class Vehicle {
void start() {
System.out.println("Vehicle is starting");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car is starting");
}
}
a) Briefly explain the following terms in the context of Java programming. Write a sample
java code statement for each case
i) Variable - A variable is a container for data used to store values during program execution.
ii) Assignment operator - assigns the value on the right-hand side to the variable on the left-
hand side. Denoted by =
E.g int number = 10;
iii) Array - An array is a collection of elements of the same type, stored in a single memory
location.
E,g int[] numbers = {1, 2, 3, 4, 5};
iv) Syntax error - A syntax error occurs when code does not follow the rules of the Java
language, such as missing semicolons or incorrect syntax.
// Step 1
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
//Step 2
public class HelloWorldApp extends Application {
//Step 3
@Override
public void start(Stage primaryStage) {
// Step 4
Label helloLabel = new Label("Hello, World!");
//Step 5
StackPane root = new StackPane();
root.getChildren().add(helloLabel);
//Step 6
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
//Step 7
public static void main(String[] args) {
launch(args);
Members