Java Classes and Objects: Exam Q&A, Exams of Java Programming

A concise overview of classes and objects in java, covering essential concepts such as method declaration, return values, access modifiers (public, package, default, protected, private), getters and setters, encapsulation, and constructors. It also includes examples of using the math class for mathematical operations and discusses value types, reference variables, packages, and object-oriented programming principles. The document serves as a quick reference or study aid for understanding fundamental java programming concepts, focusing on code reuse and object-oriented design.

Typology: Exams

2024/2025

Available from 06/26/2025

brian-mwihia
brian-mwihia 🇺🇸

1.2K documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java: Classes and Objects UPDATED ACTUAL Exam Questions
and CORRECT Answers
Declaring a method - class Myclass{
static void sayHello(){
//sayHello is the method
System.out.println("Hello World!")
}
public static void main (String [ ] args) {
sayHello();
sayHello();
sayHello();
}
}
/* output:
Hello World!
Hello World!
Hello World!
*/
code reuse - You can write a method once, and use it multiple times, without having to rewrite
the code each time
Return value syntax - class MyClass {
pf3
pf4
pf5

Partial preview of the text

Download Java Classes and Objects: Exam Q&A and more Exams Java Programming in PDF only on Docsity!

Java: Classes and Objects UPDATED ACTUAL Exam Questions

and CORRECT Answers

Declaring a method - class Myclass{ static void sayHello(){ //sayHello is the method System.out.println("Hello World!") } public static void main (String [ ] args) { sayHello(); sayHello(); sayHello(); } } /* output: Hello World! Hello World! Hello World! */ code reuse - You can write a method once, and use it multiple times, without having to rewrite the code each time Return value syntax - class MyClass {

static int sum(int val1, int val2) { return val1 + val2; } public static void main(String[ ] args) { int x = sum(2, 5); System.out.println(x); } } //output 7 *Note: The return is saying to add val 1 and val 2 (parameters), as you can see in the next function starting with public, there is a variable with x and the sum has (2,5) those two will be added due to the method prior Public - The class is accessible by any other class. Package - A group of similar types of classes Default - A variable or method declared with no access control modifier is available to any other class in the same package. Protected - Same as default, with the addition that subclasses can access protected methods and variables of the superclass (learn more about subclasses and superclass later) Private - Accessible only within the declared class itself

Reference variable - When you create an object using the constructor Math class - which provides predefined methods for mathematical operations Math.abs() - returns the absolute value of its parameter Math.ceil() - Rounds to nearest integer up when using double Math.max() - Rounds to the nearest integer down when using dobule Math.min() - Returns the smallest parameter Ex int m = Math.min(10, 20); // 10 Math.pow() - takes two parameters and returns the first parameter raised to the power of the second parameter ex double p = Math.pow(2, 3); // 8. sqrt() - square root sin() - sine cos() - cosine

final - Keeps a variable constant, any attempt to change it will cause an error ex. class MyClass { public static final double PI = 3.14; public static void main(String[ ] args) { System.out.println(PI); } } Packages - a group made up of similar types of classes, along with sub-packages OOP - Object-Oriented Programming Attributes - Characteristics in an object. For example, a car can be red or blue Behavior - Dependent on the object type Three dimensions in object oriented - Identity Attributes Behavior Class - Describes what an object will be. It is a blueprint, description, definition of an object Class syntax - Very beginning! public class (name class here) {