Content text OOP - 2022W Paper Solution(1).pdf
OOP Winter 2022 GTU Paper Solution :-) Question 1 (a) Discuss significance of byte code. Bytecode is the intermediate code that is generated by the Java compiler and executed by the Java Virtual Machine (JVM). Here are some of the significant benefits of bytecode in the context of Java: Portability: Java bytecode is platform-independent, which means that it can be executed on any platform that has a JVM installed Security: Bytecode provides an additional layer of security as it can be executed within a sandbox environment. This sandbox ensures that the bytecode cannot access system resources directly, which helps prevent malicious code from damaging the system. Performance: While bytecode requires an additional step of interpretation or compilation by the JVM, it can be optimized for performance during this step. Additionally, the JVM can dynamically adapt its execution to improve performance based on the current system load and available resources. Maintainability: Bytecode can be easily decompiled back into source code, which can help developers understand and modify existing code. (b) Explain Java garbage collection mechanism. In Java, garbage collection is an automatic memory management mechanism that frees up memory that is no longer needed by the program. It works by periodically scanning the heap, which is the region of memory where objects are stored, and identifying objects that are no longer being referenced by the program. These objects are then marked for garbage collection and their memory is released. The garbage collection process is automatic and runs in the background without any intervention from the programmer. Java uses a mark and sweep algorithm to identify objects that are no longer being referenced by the program. This algorithm works by first marking all objects that are still being referenced and then sweeping through the heap and releasing the memory of any objects that were not marked.
charAt(int index): This method returns the character at the specified index in the string. The index is zero-based, meaning that the first character in the string has an index of 0. concat(String str): This method concatenates the specified string to the end of the original string and returns a new string that represents the concatenation. substring(int beginIndex): This method returns a substring of the original string, starting at the specified index and continuing to the end of the string. indexOf(char c): This method returns the index of the first occurrence of the specified character in the string, or -1 if the character is not found. replace(char oldChar, char newChar): This method replaces all occurrences of the specified old character with the specified new character and returns a new string that represents the modified string. String str = "hello"; int len = str.length(); // len is 5 String str = "hello"; char c = str.charAt(1); // c is 'e' (c) Explain all access modifiers and their visibility as class members. In object-oriented programming, access modifiers are keywords used to set the accessibility or visibility of class members (fields, methods, and inner classes) in a class. There are four access modifiers in Java: Public: Public members are accessible to all classes, regardless of the package they belong to. Public members can be accessed by any other class in the same program. Private: Private members are only accessible within the same class. They cannot be accessed by any other class, even if it belongs to the same package. Protected: Protected members are accessible within the same class, as well as any subclass that extends the class. They cannot be accessed by any other class, including those in the same package. Default (also known as package-private): Members with no access modifier specified are only accessible within the same package. They cannot be accessed by any other class outside the package.