Java OOP Concepts Online Test
Java OOP Concepts Online Test
The class `TestingMethods5` results in a compiler error due to the use of the variable `localVariable` without initialization. Java requires all local variables to be explicitly initialized before they are used in any operation, including output. Attempting to print the `localVariable` without initialization leads to a compilation error due to violation of Java's definite assignment policy .
The `MethodOverloading1` class illustrates method overloading by defining two methods `show(int, char)` and `show(char, int)`. When `m.show(10, 'A')` is called, Java resolves the method call to `show(int, char)`, resulting in the "KING KONG" output. For the call to `m.show('B', 10)`, the method `show(char, int)` is invoked, resulting in "JIM JAM". This distinction demonstrates method overloading through different parameter type signatures .
The method `show` in the `Road` class is defined as static, meaning it belongs to the class rather than any instance. Therefore, it can be called without creating an instance of `Road`. When `Road.show();` is executed in `TestingMethods10`, it directly calls the static method, outputting "Inside static method." to the console .
In the `Constructor3` class, the no-argument constructor `Constructor3()` calls another constructor in the same class using `this(20)`, passing the integer `20` as an argument. The constructor `Constructor3(int birds)` receives this call and uses it to print "Birds=" followed by the passed argument `20`. Consequently, the output shows "Birds=20" .
In the Testing2 Java class, the object `t1` of class `Fox` initially has the instance variable `legs` set to 2. When the line `t1.legs = 4;` is executed within the `main` method, it modifies the object reference `t1`, changing the `legs` value to 4. Since `t1` is an object reference, modifying its fields affects the original object, leading to the output of "T1 before: 2, T1 After: 4" .
Static variables in Java are shared across all instances of a class, meaning they maintain a single copy that is accessible by any object of the class. In `TestingMethods6`, when `t6` sets `cats` to 10 through `t7.cats = 10;`, it updates the single shared static variable. As a result, when `t6` accesses `cats` again, it reflects the updated value of 10, demonstrating the shared nature of static variables across different instances .
Java follows 'pass by value', meaning when a primitive type like `int` is passed to a function, a copy of the actual value is passed but not the original variable. In the `Testing10` class, the method `modify(int r)` changes the value of `r` to 20, however, this modification affects only the local copy of `r`, and not the original `rats` variable of the object `t1`. Therefore, when `rats` is printed afterward, it remains unchanged at its original value .
In Java, when an object is passed to a method, it is passed by value, but this value is the reference to the object. In the `Food` class example, the method `change(Food foo)` modifies the `items` field of the `foo` object by changing it to 10. This alteration persists after the method execution because the reference to the object `f` was passed, allowing `foo` to modify the same object's internal state externally via its reference .
In the `Cricket` class example, `c1` and `c2` both refer to the same `Cricket` object. Thus, altering `c2.runs` to 300 directly changes the `runs` value of the object that `c1` refers to as well, showing "Runs= 300". This contrast with primitive types, where passing modifies only local copies, illustrates how altering object references impacts original objects since multiple variables can reference the single underlying object .
In Java, the method naming convention following the class name, such as `void TestingConstructor()`, is mistakenly taken as a method due to the presence of the `void` keyword and not considered a constructor. Conversely, `TestingConstructor()` without a return type is considered a legitimate constructor. Therefore, when a `TestingConstructor` object is instantiated, the actual `constructor` is invoked, producing the output "Antarctica" as defined .