0% found this document useful (0 votes)
80 views5 pages

Java OOP Concepts Online Test

Uploaded by

Gomathy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views5 pages

Java OOP Concepts Online Test

Uploaded by

Gomathy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

07/10/2024, 10:29 Java Test 3 07/10/2024, 10:29 Java Test 3

3. What is the output of the below Java program? *


//[Link] file
Java Test 3 public class Hello
online test on OOP concepts and methods {
public static void main(String[] args)
* Indicates required question
{
[Link]("BINGO");
1. Batch No * }
}

Mark only one oval.

2. What is the output of the below Java program with two classes? //[Link] * A) bingo

public class Example B) BINGO


{ C) Compiler error

D) None
}
public class Testing1
{
public static void main(String[] args) 4. What is the output of the below java class? *
{ class Fox
[Link]("Hello Boss.!"); {
} int legs = 2;
} }
class Testing2
Mark only one oval.
{
A) Hello Boss.! public static void main(String[] args)
{
B) No Output
Fox t1 = new Fox();
C) Compiler error
[Link]("T1 before: " + [Link]);
D) None of the above [Link] = 4;
[Link]("T1 After: " + [Link]);
}
}

Mark only one oval.

T1 before: 4, T1 After :4

T1 before: 2, T1 After :4

Compiler error

T1 before: 2, T1 After :2

[Link] 1/17 [Link] 2/17

07/10/2024, 10:29 Java Test 3 07/10/2024, 10:29 Java Test 3

5. A primitive variable is passed from one method to another method by ___ * 7. What is the output of the below Java program that passes an object to another *
in Java. method?
class Food
Mark only one oval.
{
A) Pass by value int items;
int show()
B) Pass by reference
{return items;}
}

6. An object or a primitive value that is received in a method from another * class Testing9
method is called ___ in Java. (Argument / Parameter) {
public static void main(String[] args)
Mark only one oval. {
Food f = new Food();
A) Argument
[Link] = 5;
B) Parameter [Link]("Items Before = " + [Link]());
change(f);
[Link]("Items After = " + [Link]());
}
static void change(Food foo)
{ [Link] = 10; }
}

Mark only one oval.

Items Before = 10 Items After = 10

Items Before = 5 Items After = 5

Items Before = 5 Items After = 10

Items Before = 5 Items After = 10

[Link] 3/17 [Link] 4/17


07/10/2024, 10:29 Java Test 3 07/10/2024, 10:29 Java Test 3

8. What is the output of the below Java program that passes primitive values? * 10. What is the output of the below Java program? *
class Testing10 class Cricket
{ { int runs; }
int rats = 5;
class Testing19
public static void main(String[] args) {
{ public static void main(String[] args)
Testing10 t1 = new Testing10(); {
[Link]("Rats Before = " + [Link]); Cricket c1 = new Cricket();
modify([Link]); [Link] = 250;
[Link]("Rats After = " + [Link]); Cricket c2;
} c2 = c1;
static void modify(int r) [Link] = 300;
{ r = 20; } [Link]("Runs= " + [Link]);
} }
}
Mark only one oval.
Mark only one oval.
Rats Before = 5 Rats After = 5

Rats Before =20 Rats After = 20 A) Runs= 0

Rats Before = 5 Rats After = 20 B) Runs= 250

Rats Before =20 Rats After = 5 C) Runs= 300

D) Compiler error

9. In Java Pass by reference ___ is passed even if you are passing a *


reference to an object.

Mark only one oval.

A) Address value

B) Variable value

C) Hash code

D) None of the above

[Link] 5/17 [Link] 6/17

07/10/2024, 10:29 Java Test 3 07/10/2024, 10:29 Java Test 3

11. What is the output of the below Java program? * 13. Java method signature is a combination of ___. *
class Wordpress
Mark only one oval.
{ int posts; }
class Testing20 A) Return type
{
B) Method name
public static void main(String[] args)
{ C) Argument List
Wordpress wp1 = new Wordpress(); D) All the above
[Link] = 25; B and C
Wordpress wp2 = wp1;
wp1 = null;
[Link]("Posts=" + [Link]);
} 14. What is the output of the below Java program with an empty return statement? *
} public class TestingMethods2
{
Mark only one oval.
void show()
A) Posts=25
{
[Link]("SHOW Method..");
B) Posts=0
return;
C) Posts=null }
D) Runtime exception occurs public static void main(String[] args)
{
TestingMethods2 t2 = new TestingMethods2();
[Link]();
12. State TRUE or FALSE. A Java method can have the same name as the class * }
name. }

Mark only one oval. Mark only one oval.

A) TRUE A) SHOW Method..


B) FALSE B) No output

C) Compiler error

D) None

[Link] 7/17 [Link] 8/17


07/10/2024, 10:29 Java Test 3 07/10/2024, 10:29 Java Test 3

15. A "this" operator used inside a Java method refers to ___ variable. * 18. What is the output of the below Java program? *
class Road
Mark only one oval.
{
A) Global variable static void show()
{
B) Method local variable
[Link]("Inside static method.");
C) Instance variable }
D) None }

public class TestingMethods10


{
16. What is the output of the below Java program? * public static void main(String[] args)
public class TestingMethods5 {
{ [Link]();
public static void main(String[] args) }
{ }
int localVariable;
[Link](localVariable); Mark only one oval.
}
A) Inside static method.
}
B) empty message
Mark only one oval.
C) Compiler error
A) 0 D) Runtime error / exception
B) garbage value

C) NullPointerException

D) Compiler error

17. In Java, local variables are stored in __ memory and instance variables are *
stored in ___ memory.

Mark only one oval.

A) Stack, Stack

B) Heap, Heap

C) Stack, Heap

D) Heap, Stack

[Link] 9/17 [Link] 10/17

07/10/2024, 10:29 Java Test 3 07/10/2024, 10:29 Java Test 3

19. What is the output of the Java program with static variables? * 21. What is the output of the below Java program? *
public class TestingMethods6 public class TestingConstructor
{ {
static int cats=25; void TestingConstructor()
public static void main(String[] args) {
{ [Link]("Amsterdam");
TestingMethods6 t6 = new TestingMethods6(); }
[Link]("t6 BIRDS before=" + [Link]);
TestingMethods6 t7 = new TestingMethods6(); TestingConstructor()
[Link] = 10; {
[Link]("t6 BIRDS after=" + [Link]); [Link]("Antarctica");
} }
}
public static void main(String[] args)
Mark only one oval.
{
t6 BIRDS before=25 t6 BIRDS after=25 TestingConstructor tc = new TestingConstructor();
}
t6 BIRDS before=25 t6 BIRDS after=10
}
t6 BIRDS before=25 t6 BIRDS after=0
Mark only one oval.
None

A) Antarctica

B) Amsterdam
20. In Java, a constructor with no parameters or no arguments is called ___ * C) No output
constructor.
D) Compiler error

Mark only one oval.

A) Default constructor

B) User-defined constructor

[Link] 11/17 [Link] 12/17


07/10/2024, 10:29 Java Test 3 07/10/2024, 10:29 Java Test 3

22. What is the output of the below Java program with overloaded constructors? * 23. Choose the correct way of calling the second constructor from the first *
public class Constructor3 constructor in the below code options.
{ A)
int birds=10; Constructor5()
Constructor3() {
{ int a=30;
this(20); this('A');
} }
Constructor3(int birds) Constructor5(char c)
{ {
[Link]("Birds=" + birds); //
} }

public static void main(String[] args) B)


{ Constructor5()
Constructor3 con = new Constructor3(); {
} int a=30;
} this('A');
[Link]("Success");
Mark only one oval.
}
A) Birds=0 Constructor5(char c)
{
B) Birds=10
//
C) Birds=20 }
D) Compiler error
C)
Constructor5()
{
this('A');
[Link]("Success");
}
Constructor5(char c)
{
//
}

D) All the above

Mark only one oval.

A and B

A, B, C

[Link] 13/17 [Link] 14/17

07/10/2024, 10:29 Java Test 3 07/10/2024, 10:29 Java Test 3

All are correct


25. What is the output of the below Java program with multiple methods? *
public class MethodOverloading1
{
24. What is the output of the below Java program with many constructors? * void show(int a, char b)
public class Constructor8 {
{ [Link]("KING KONG");
Constructor8(boolean a) }
{
[Link]("MODEM="+ a ); void show(char a, int b)
} {
Constructor8(float a) [Link]("JIM JAM");
{ }
[Link]("ROUTER=" + a);
} public static void main(String[] args)
public static void main(String[] args) {
{ MethodOverloading1 m = new MethodOverloading1();
Constructor8 con1 = new Constructor8(50); [Link](10, 'A');
Constructor8 con2 = new Constructor8(false); [Link]('B', 10);
} }
} }

Mark only one oval. Mark only one oval.

ROUTER=50.0 MODEM=false JIM JAM JIM JAM

ROUTER=50 MODEM=false KING KONG KING KONG

Compile error JIM JAM KING KONG

None compiler error

This content is neither created nor endorsed by Google.

Forms

[Link] 15/17 [Link] 16/17


07/10/2024, 10:29 Java Test 3

[Link] 17/17

Common questions

Powered by AI

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 .

You might also like