2023 Assesment Exam Flashcards
Define syntax
refers to the set of rules that define the structure of a program
Define Java API
Application Programming Interface, is a collection of pre-written software components that developers can use in their Java programs.
Define a Variable
a named storage location in memory that holds a value during program execution.
Define source code
the set of instructions written by a programmer using the Java programming language.
Define an Algorithm
set of well-defined instructions that are executed in a specific order to solve a particular problem
State examples of eselctive control structures
sequence , The If statement , the Switch(case) statement , iteration loops(For Loop, Do-while loop, while loop)
Assume that there is a java class called Test and it has not been compiled but we want to run it.
i. Identify the appropriate command to use to compile this class on command line interface.
javac Test.java
Assume that there is a java class called Test and it has not been compiled but we want to run it.
java Test
Use one java instruction to write a statement that displays the following output on the screen.
Hello
World
System.out.println(“Hello”+”\n”+”World”);
Identify (3) rules which a programmer must consider when using a loop that should terminate at some point.
- INITIALIZE the loop control variable outside the loop
- Use the loop control variable as a variable in the loop
- Alter the loop control variable
Identify a loop which should be used in a situation where it should execute at least once.
Do while statement
Justify why statements in the body of a loop must be indented.
- Improve legibility
- Improve readability
- Neatness
Define Sub routines
Methods are essentially self-contained blocks of code that perform specific tasks within a class.
Define a constructor
a constructor is a special type of method that is called automatically whenever you create an object of a class using the “new” keyword.
Give an example demonstrating how subroutines/methods are defined in Java
public static void add(int a, int b ){
int sum;
sum = a + b;
}
Differentiate between a void and non-void subroutine?
A void method does not return a value while a non void returns a value.
Why is it important to use parameterized constructors.
because they allow you to create objects with specific initial values, making your objects more flexible and customizable.
What is the purpose of assignment operators in Java? Give an example.
assign values to variables. e,g
int age = 25
How can you use the += operator in Java to increment a variable’s value by a specified amount? Give an example.
The += operator in Java is a shorthand way to increment a variable by a specified amount. It combines addition and assignment.
e,g
int count = 5;
count += 3; // Equivalent to count = count + 3; // Now count will be 8
What is implicit cast in Java? Provide an example of implicit
Implicit casting (widening conversion): Automatic conversion of smaller data types to larger ones (e.g., int to double). No data loss, happens automatically.
int x = 10; double d = x; // No cast needed
What is explicit cast in Java? Provide an example of explicit.
Explicit casting (narrowing conversion): Manual conversion of larger data types to smaller ones (e.g., double to int). Possible data loss, requires the cast operator (type).
double d = 3.14; int x = (int) d; // Possible data loss (decimal part truncated)
Define a literal in Java and provide examples of 5 different types of literals.
A literal in Java is a fixed value directly written in your code. It represents a constant data that cannot be changed during program execution.
e,g ; integer , character , String , Double , Float, Boolean.
explain 4 primitive data types
Primitive Data Types: These are basic building blocks that hold specific data types and have predefined sizes in memory.:
byte: Stores small whole numbers (8 bits).
short: Stores whole numbers (16 bits).
int: Stores whole numbers (32 bits, commonly used).
long: Stores large whole numbers (64 bits).
explain reference data types
Non-primitive Data Types: These are reference types that refer to objects in memory. They are more complex and user-defined. Examples include:
String: Represents text data.