2017 ass java Flashcards

1
Q

Explain what are (i) static variables, (ii) static methods (iii) static constant

A

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

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.

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between a void method and a value-returning method [3marks]

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give any four (4) rules that must be followed when calling a method

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between a do-while loop and a while loop?

A

do while loops atleast once wonce while loops until condition is false;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the difference between an object and a class in Java

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

if a class has a private field, which elements would have access to the field?

A

only methods within the same class can access the field directly. This enforces encapsulation and data hiding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

if a class has a private field, which elements would have access to the field?

A

only methods within the same class can access the field directly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is variable declaration and how is it different from variable initialization [5 marks]

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

c) What is the difference between a global and local variable?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

a) List any 6 of the java primitive data types

A

byte (8-bit signed integer)
short
int
long
float
double

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Java has three control structures, use examples to describe them (name and give example for each)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly