2017 Final Exam Flashcards
What does the term method-overloading mean? Give an example with two methods of your choice. [6 marks]
Method overloading allows creating multiple methods with the same name in a class, but with different parameters (number or data type).
It promotes code readability and reusability.
calculateArea(double radius)
for circles,
calculateArea(int length, int width) for rectangles.
iii) What are static methods in java and when do we use them? [5 marks]
Static methods in Java are part of the class itself, not an object of the class. You can call them without creating an object. They are useful for helper functions or functionality that doesn’t depend on a specific object’s state.
Explain the term encapsulation and discuss two advantages of encapsulation in Objected Oriented Programming.
refers to bundling data with methods that can operate on that data within a class.
information hiding from external classes helps the programmer keep control of your program.
information hiding from external classes helps prevent the program form ending up in any strange or unwanted states.
iii) Explain with an example line of code the significance of the final modifier to a class, variable and method.
class MathUtil { // final class - cannot be inherited
public static final double PI = 3.14159; // final variable - constant value
public static final int add(int x, int y) { // final method - cannot be overridden
return x + y;
}
}
iv) Find the error in the following method definition:
public static int half (double num){
double result = num/2.0;
return result;
}
The error is the return type. The method is declared to return an int (whole number), but the calculation and variable result are of type double (decimal).
fix:
public static int half(double num) {
return (int) (num / 2.0); // Cast the result to an int before returning
}
If the method returns int, int num = 2 what would be the result at the end?
1
Explain what are input or source streams and name all 2 root classes of input streams.
In Java, input streams represent sources of data that your program can read from. There are two root classes for input streams:
InputStream: This is the most general class for byte-oriented input streams. It provides basic methods for reading bytes from various sources like files, keyboards, or network connections. Reader: This class is for character-oriented input streams. It deals with data in characters instead of raw bytes and offers methods for reading characters while handling character encoding.
Explain what is a char data type?
is a primitive data type used to store a single Unicode character. It occupies 16 bits (2 bytes) and can represent any character from a vast range, including letters, numbers, and symbols.
What is a Character class?
is a wrapper class
for the primitive char data type. It provides functionality beyond storing a single character:
Object Representation: It allows you to treat a character as an object, useful in situations where objects are required. Character Manipulation Methods: It offers various static methods for manipulating characters, such as checking case (uppercase/lowercase), digit or letter identification, and conversion between uppercase and lowercase
iii. Explain the following methods found in the Character class;
[10 marks]
a. isUpperCase()
b. toUpperCase()
c. isLowerCase()
d. toLowerCase()
e. isDigit()
isUpperCase(char ch)
: Checks if the character ch is an uppercase letter (A-Z) and returns true if yes, false otherwise.
toUpperCase(char ch): Converts the character ch to its uppercase equivalent if it’s a lowercase letter (a-z). Returns the original character if it’s already uppercase or not a letter.
isLowerCase(char ch): Checks if the character ch is a lowercase letter (a-z) and returns true if yes, false otherwise.
toLowerCase(char ch): Converts the character ch to its lowercase equivalent if it’s an uppercase letter (A-Z). Returns the original character if it’s already lowercase or not a letter.
isDigit(char ch): Checks if the character ch is a digit (0-9) and returns true if yes, false otherwise.
iv. Differentiate between the following String class methods;
a. Equals() and equalsIgnoreCase(); [2 marks]
b. toUpperCase() and toLowerCase() methods [2 marks]
c. The endsWith() and startsWith() [2 marks]
a. equals() vs. equalsIgnoreCase()
equals(): Checks for exact string equality, considering both case and characters. equalsIgnoreCase(): Checks for string equality, ignoring the case of characters.
b. toUpperCase() vs. toLowerCase()
toUpperCase(): Converts all characters in the string to uppercase. toLowerCase(): Converts all characters in the string to lowercase.
c. endsWith() vs. startsWith()
endsWith(String suffix): Checks if the string ends with the specified suffix. startsWith(String prefix): Checks if the string starts with the specified prefix.
Name 2 options plus the Java class of reading keyboard input into your program.
Scanner class
Console
java.util.Scanner;
i) What is an I/O stream?
An I/O stream in Java is a pathway for data to flow between your program and external sources/destinations (files, devices, etc.) for reading or writing.
ii) A stream can represent many different kinds of sources and destinations. Name any 4 sources or destinations.
Files
Network connections
Standard input (keyboard)
Memory arrays