2017 ass java Flashcards
Explain what are (i) static variables, (ii) static methods (iii) static constant
(i) Static Variables:
Belong to the class itself, not individual objects.
There’s only one copy shared by all objects of the class.
(ii) Static Methods:
Can be called without creating an object of the class.
Operate on the class itself or other static elements.
(iii) Static Constants:
Static variables with a fixed value.
Cannot be changed after initialization.
c) With an aid of java code example explain the following access modifiers [6 marks]
and discuss how they are used in java:
i. private
ii. public.
private:
Restricts access to the member (variable, method) within the class itself.
Other classes cannot directly access private members.
public class Car {
private String model;
public:
Grants access to the member from anywhere in the program.
Public members are visible and usable by other classes.
public class Circle {
public double radius;
What is the difference between a void method and a value-returning method [3marks]
Void Method: Performs an action but doesn’t return any value. Often used for tasks like printing or modifying internal state.
Value-Returning Method: Executes an operation and returns a specific data type (e.g., int, String). Used to provide calculated results or retrieved data.
Give any four (4) rules that must be followed when calling a method
The method must be defined in the class or a superclass.
The number and types of arguments passed to the method must match the method’s parameter list.
or non-static methods, you need to create an object of the class before calling the method.
the method must be accessible from the code where you’re calling it (consider public vs. private modifiers).
Specify the type of data the variable will hold (e.g., int, String).
Unique name: Choose a unique name that follows Java naming conventions (letters, numbers, underscores).
optional initialization: You can assign an initial value during declaration (e.g., int age = 25;).
Scope: Consider where the variable is declared (local vs. class-level) to determine its accessibility.
What is the difference between a do-while loop and a while loop?
do while loops atleast once wonce while loops until condition is false;
Explain the difference between an object and a class in Java
Class: A blueprint or template that defines the properties (variables) and behaviors (methods) of objects.
Object: An instance of a class. It holds specific values for the properties defined in the class and can access the class’s methods.
if a class has a private field, which elements would have access to the field?
only methods within the same class can access the field directly. This enforces encapsulation and data hiding.
if a class has a private field, which elements would have access to the field?
only methods within the same class can access the field directly
What is variable declaration and how is it different from variable initialization [5 marks]
Variable Declaration:
Creates a space in memory for the variable and specifies its data type (e.g., int age;).
Reserves memory but doesn’t assign a value.
Variable Initialization:
Assigns a specific value to the declared variable (e.g., int age = 25;).
Provides the initial data the variable will hold.
Difference:
Declaration allocates memory and defines the type, while initialization sets the starting value.
c) What is the difference between a global and local variable?
Global Variable:
Declared outside any function or block, often at the beginning of the program.
Accessible from anywhere in the program.
Generally discouraged due to potential naming conflicts and difficulty managing code.
Local Variable:
Declared inside a function or block (like an if statement).
Only accessible within the function or block where it’s declared.
Promotes better code organization and reduces risk of unintended modifications.
a) List any 6 of the java primitive data types
byte (8-bit signed integer)
short
int
long
float
double
Java has three control structures, use examples to describe them (name and give example for each)
Selection: Used for decision-making based on conditions. e.g if else statements
Iteration: Used for repeating a block of code a specific number of times or until a condition is met. loops
sequential