Java Object-Oriented Programming Exam Questions
Java Object-Oriented Programming Exam Questions
Object-oriented programming (OOP) is characterized by encapsulation, inheritance, and polymorphism. Encapsulation involves bundling data with the methods that operate on this data. Inheritance allows creating new classes that are based on existing ones, promoting code reuse. Polymorphism enables the calling of methods in derived classes through their base class references, allowing for method overriding. These features distinguish OOP by promoting modular, maintainable, and reusable code .
Type conversion in Java refers to converting one data type to another, which could be implicit or explicit. Implicit conversion, or 'widening', occurs when a smaller data type is automatically converted to a larger type, while explicit conversion or 'casting' requires manual intervention using a cast operator, especially when converting a larger type to a smaller one ('narrowing'). For example, assigning an int to a double is implicit, while converting a double to an int involves explicit casting using (int). Understanding these conversions is crucial to manage type compatibility and precision in Java programs .
Lexical issues in Java refer to problems related to the syntax and the tokens utilized within the code. Common lexical issues include incorrect usage of keywords, invalid identifiers (e.g., starting identifiers with numbers), and improper handling of literals and comments. These issues can lead to compilation errors since the compiler fails to recognize malformed tokens or syntax errors within the source code. Ensuring that code adheres to Java's lexical syntax is crucial for successful compilation and execution .
Java achieves platform independence through its use of the Java Virtual Machine (JVM). When Java source code is compiled, it is transformed into bytecode by the Java compiler. This bytecode is not machine-specific; instead, it can be executed by any JVM, which is available for most platforms. This architecture allows Java programs to be written on one platform and executed on any other platform with a compatible JVM, thus fulfilling the 'write once, run anywhere' capability .
Bitwise operators, like '&', '|', '^', are used to perform operations on binary representations of integers. They are utilized for low-level programming tasks, such as bit manipulation. In contrast, short-circuit logical operators, '&&' and '||', evaluate conditional expressions. Short-circuits can bypass unnecessary evaluations since expressions are evaluated only as needed to determine a condition's truth value, which often results in performance optimizations in complex conditional statements .
In Java, the '<<' operator performs a bitwise left shift, moving bits to the left and inserting zeros on the right, equivalent to multiplying by powers of two. The '>>' operator conducts an arithmetic right shift, moving bits to the right while preserving the sign bit on the left, effectively dividing by powers of two while keeping sign. The '>>>' is a logical right shift, which also shifts bits right but inserts zeros regardless of sign, which is useful in unsigned context operations. An example is shifting an integer to manipulate data in tasks like checksum calculations .
Control statements in Java guide the flow of execution based on certain conditions, allowing for more dynamic and responsive programs. The primary control statements include decision-making statements like 'if', 'else-if', and 'switch', which allow executing code blocks based on conditional expressions. For iteration, Java offers loops such as 'for', 'while', and 'do-while', enabling repeated execution of code blocks. Additionally, 'break' and 'continue' statements provide finer control within loops. The use of these control structures can be illustrated through examples like using a 'for' loop to iterate over array elements or an 'if-else' statement to check and execute code based on specific conditions .
The ternary operator is a shorthand for the 'if-else' statement in Java, used for decision-making with the syntax `expression ? value_if_true : value_if_false`. For finding the greatest of three numbers, the operator can be nested as: `int greatest = (a > b) ? (a > c ? a : c) : (b > c ? b : c);`. This concise form compares 'a' and 'b', then the result against 'c' to determine the maximum number with fewer lines of code than traditional nested 'if-else' structures .
Java variables have four types of scopes: local, class/static, instance, and method. Local variables are declared within a method and are accessible only within that method. Class/static variables are declared with the static keyword inside a class, shared among all instances, and accessible through the class name. Instance variables are fields within a class accessed through class instances. Method parameters are also local, with scope limited to the method. These scoping rules determine the visibility and lifetime of variables, crucial for code organization and avoiding conflicts .
Arrays in Java are used to store multiple values of the same type in a contiguous memory location, allowing easy management and access via indices. A 1D array is declared as `type[] arrayName = new type[size];`. For a 2D array, it's declared as `type[][] arrayName = new type[rows][columns];`. For example, `int[] numbers = {1, 2, 3};` initializes a 1D array, while `int[][] matrix = {{1, 2}, {3, 4}};` initializes a 2D array. Arrays are significant in simplifying data handling, enabling batch processing with loops .