0% found this document useful (0 votes)
21 views

OOP Quiz

Uploaded by

mostafa nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

OOP Quiz

Uploaded by

mostafa nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Java Constructors

Question 1

Is there any compiler error in the below Java program?

Java

class Point {
int m_x, m_y;
public Point(int x, int y) { m_x = x; m_y = y; }
public static void main(String args[])
{
Point p = new Point();
}
}

Yes

No

Discuss it

Question 1 ‒ Explanation
The main function calls parameterless constructor, but there is only one constructor defined in class which takes two
parameters. Note that if we write our own constructor, then compiler doesn\'t create default constructor in Java. Thi
s behavior is same as C++.

Question 2

JAVA

class Test
{
static int a;

static
{
a = 4;
System.out.println ("inside static block\\n");
System.out.println ("a = " + a);
}

Test()
{
System.out.println ("\\ninside constructor\\n");
a = 10;
}

public static void func()


{
a = a + 1;
System.out.println ("a = " + a);
}

public static void main(String[] args)


{

Test obj = new Test();


obj.func();

}
}

inside static block


a = 4
inside constructor
a = 11

Compiler Error

Run Time Error

inside static block


a = 4
inside constructor
a = 5

inside static block


a = 10
inside constructor
a = 11

Discuss it

Question 2 ‒ Explanation
Static blocks are called before constructors. Therefore, on object creation of class Test, static block is called. So, s
tatic variable a = 4. Then constructor Test() is called which assigns a = 10. Finally, function func() increments its v
alue.

Question 3

Java

final class Complex {


private double re, im;
public Complex(double re, double im) {
this.re = re;
this.im = im;
}
Complex(Complex c)
{
System.out.println("Copy constructor called");
re = c.re;
im = c.im;
}
public String toString() {
return "(" + re + " + " + im + "i)";
}
}
class Main {
public static void main(String[] args) {
Complex c1 = new Complex(10, 15);
Complex c2 = new Complex(c1);
Complex c3 = c1;
System.out.println(c2);
}
}

Copy constructor called


(10.0 + 15.0i)

Copy constructor called


(0.0 + 0.0i)

(10.0 + 15.0i)

(0.0 + 0.0i)

Discuss it

Question 4

Output of following Java program

Java

class Point {
int m_x, m_y;

public Point(int x, int y) { m_x = x; m_y = y; }


public Point() { this(10, 10); }
public int getX() { return m_x; }
public int getY() { return m_y; }

public static void main(String args[]) {


Point p = new Point();
System.out.println(p.getX());
}
}

10

compiler error

Discuss it
Question 4 ‒ Explanation
it\'s a simple program where constructor is called with parameters and values are initialized.

Question 5

Which of the following is/are true about constructors in Java?

1) Constructor name should be same as class name.


2) If you don\'t define a constructor for a class,
a default parameterless constructor is automatically
created by the compiler.
3) The default constructor calls super() and initializes all
instance variables to default value like 0, null.
4) If we want to parent class constructor, it must be called in
first line of constructor.

1, 2

1, 2 and 3

1, 2, 3 and 4

Discuss it

Question 6

Predict the output?

Java

package main;
class T {
int t = 20;
}
class Main {
public static void main(String args[]) {
T t1 = new T();
System.out.println(t1.t);
}
}

20

Compiler Error
Discuss it

Question 6 ‒ Explanation
In Java, member variables can assigned a value with declaration. In C++, only static const variables can be assign
ed like this.

Question 7

Predict the output of following Java program

Java

class T {
int t = 20;
T() {
t = 40;
}
}
class Main {
public static void main(String args[]) {
T t1 = new T();
System.out.println(t1.t);
}
}

20

40

Compiler Error

Discuss it

Question 7 ‒ Explanation
The values assigned inside constructor overwrite the values initialized with declaration.

You have completed 7/7 questions .


Your accuracy is 85%.

Last Updated : Mar 22, 2024


Take a part in the ongoing discussion View All Discussion

Company Languages DSA Data Web Python


About Us Python Data Science & Technologies Tutorial
Corporate & Legal Java Structures
Communications Address:- In Media
ML HTML Python
C++ Algorithms CSS Programming
A-143, 9th Floor, Sovereign Data Science
Corporate Tower, Sector- Contact Us PHP DSA for JavaScript Examples
With Python
136, Noida, Uttar Pradesh Advertise with GoLang Beginners TypeScript Python Projects
(201305) | Registered Data Science
us SQL Basic DSA ReactJS Python Tkinter
Address:- K 061, Tower K, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Gulshan Vivante Apartment, Solution
Machine
Sector 137, Noida, Gautam Android DSA Bootstrap OpenCV Tutorial
Learning
Buddh Nagar, Uttar Pradesh, Placement Tutorial Roadmap Web Design Python Interview
ML Maths
201305 Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Digital Logic Patterns Aptitude
Design OOAD Preparation
Engineering System Puzzles
Maths Design
Software Bootcamp
Development Interview
Software Questions
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved


Java Inheritance Trending in News View More

105 Funny Things to Do to Make


Question 1 Someone Laugh
Best PS5 SSDs in 2024: Top Picks for
Expanding Your Storage
Which of the following is true about inheritance in Java?
Best Nintendo Switch Controllers in
2024
1) Private methods are final. #geekstreak2024 – 21 Days POTD
2) Protected members are accessible within a package and Challenge Powered By Deutsche Bank
inherited classes outside the package. Full Stack Developer Roadmap [2024
3) Protected methods are final. Updated]
4) We cannot override private methods.

1, 2 and 4

Only 1 and 2

1, 2 and 3

2, 3 and 4

Discuss it

Question 1 ‒ Explanation
See http://www.geeksforgeeks.org/can-override-private-methods-java/ and htt
p://www.geeksforgeeks.org/comparison-of-inheritance-in-c-and-java/

Question 2

Output of following Java program?

Java

class Base {
public void Print() {
System.out.println("Base");
}
}

class Derived extends Base {


public void Print() {
System.out.println("Derived");
}
}

class Main{
public static void DoPrint( Base o ) {
o.Print();
}
public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}

Base
Derived
Derived

Base
Base
Derived

Base
Derived
Base

Compiler Error

Discuss it

Question 2 ‒ Explanation
See question 1 of
http://www.geeksforgeeks.org/output-of-java-program-set-2/

Question 3

Predict the output of following program. Note that fun() is public in base and
private in derived.

Java

class Base {
public void foo() { System.out.println("Base"); }
}

class Derived extends Base {


private void foo() { System.out.println("Derived"); }
}

public class Main {


public static void main(String args[]) {
Base b = new Derived();
b.foo();
}
}

Base

Derived

Compiler Error

Runtime Error

Discuss it
Question 3 ‒ Explanation
It is compiler error to give more restrictive access to a derived class function w
hich overrides a base class function.

Question 4

Predict the output of following Java Program

Java

// filename Main.java
class Grandparent {
public void Print() {
System.out.println("Grandparent\'s Print()");
}
}

class Parent extends Grandparent {


public void Print() {
System.out.println("Parent\'s Print()");
}
}

class Child extends Parent {


public void Print() {
super.super.Print();
System.out.println("Child\'s Print()");
}
}

public class Main {


public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}

Compiler Error in super.super.Print()

Grandparent\'s Print()
Parent\'s Print()
Child\'s Print()

Runtime Error

Discuss it

Question 4 ‒ Explanation
In Java, it is not allowed to do super.super. We can only access Grandparent\'s
members using Parent. For example, the following program works fine.

// Guess the output


// filename Main.java
class Grandparent {
public void Print() {
System.out.println("Grandparent\'s Print()");
}
}
class Parent extends Grandparent {
public void Print() {
super.Print();
System.out.println("Parent\'s Print()");
}
}

class Child extends Parent {


public void Print() {
super.Print();
System.out.println("Child\'s Print()");
}
}

class Main {
public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}

Question 5

Output of following Java Program?

Java

class Base {
public void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public void show() {
System.out.println("Derived::show() called");
}
}

public class Main {


public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}

Derived::show() called

Base::show() called

Discuss it

Question 5 ‒ Explanation
In the above program, b is a reference of Base type and refers to an abject of De
rived class. In Java, functions are virtual by default. So the run time polymorph
ism happens and derived fun() is called.

Question 6

Which of the following is true about inheritance in Java. 1) In Java all classes
inherit from the Object class directly or indirectly. The Object class is root of all
classes. 2) Multiple inheritance is not allowed in Java. 3) Unlike C++, there is
nothing like type of inheritance in Java where we can specify whether the
inheritance is protected, public or private.

1, 2 and 3

1 and 2

2 and 3

1 and 3

Discuss it

Question 6 ‒ Explanation
See Comparison of Inheritance in C++ and Java

Question 7

final class Complex {

private final double re;


private final double im;

public Complex(double re, double im) {


this.re = re;
this.im = im;
}

public String toString() {


return "(" + re + " + " + im + "i)";
}
}

class Main {
public static void main(String args[])
{
Complex c = new Complex(10, 15);
System.out.println("Complex number is " + c);
}
}

Complex number is (10.0 + 15.0i)


Compiler Error

Complex number is SOME_GARBAGE

Complex number is Complex@8e2fb5


Here 8e2fb5 is hash code of c

Discuss it

Question 7 ‒ Explanation
See http://www.geeksforgeeks.org/overriding-tostring-method-in-java/

Question 8

Java

class Base {
final public void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public void show() {
System.out.println("Derived::show() called");
}
}

class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}

Base::show() called

Derived::show() called

Compiler Error

Runtime Error

Discuss it

Question 8 ‒ Explanation
Final methods cannot be overridden. See the compiler error
here
.

Question 9
\Java\

class Base {
public static void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public static void show() {
System.out.println("Derived::show() called");
}
}

class Main {
public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}

Base::show() called

Derived::show() called

Compiler Error

Discuss it

Question 9 ‒ Explanation
Like C++, when a function is static, runtime polymorphism doesn\'t happen.

You have completed 9/9 questions .


Your accuracy is 55%.

Last Updated : Mar 22, 2024

Take a part in the ongoing discussion View All Discussion


Company Languages DSA Data Web Python
About Us Python Data Science & Technologies Tutorial
Corporate & Communications Legal Java Structures
Address:- A-143, 9th Floor, In Media
ML HTML Python
C++ Algorithms CSS Programming
Sovereign Corporate Tower, Data Science
Sector- 136, Noida, Uttar Contact Us PHP DSA for JavaScript Examples
With Python
Pradesh (201305) | Registered Advertise with GoLang Beginners TypeScript Python Projects
Address:- K 061, Tower K, Data Science
us SQL Basic DSA ReactJS Python Tkinter
Gulshan Vivante Apartment, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Sector 137, Noida, Gautam Solution
Machine
Buddh Nagar, Uttar Pradesh, Android DSA Bootstrap OpenCV Tutorial
Learning
201305 Placement Tutorial Roadmap Web Design Python Interview
ML Maths
Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Digital Logic Patterns Aptitude
Design OOAD Preparation
Engineering System Puzzles
Maths Design
Software Bootcamp
Development Interview
Software Questions
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved


Java Abstract Class and Interface Trending in News View More

105 Funny Things to Do to Make


Question 1 Someone Laugh
Best PS5 SSDs in 2024: Top Picks for
Expanding Your Storage
Which of the following statement(s) with regard to an abstract class in
Best Nintendo Switch Controllers in
JAVA is/are TRUE ? I. An abstract class is one that is not used to create
2024
objects. II. An abstract class is designed only to act as a base class to be
#geekstreak2024 – 21 Days POTD
inherited by other classes. Challenge Powered By Deutsche Bank
Full Stack Developer Roadmap [2024
Only I
Updated]
Only II

Neither I nor II

Both I and II

Discuss it

Question 1 ‒ Explanation
Abstract data class is not used to create objects in Java and it is design
ed only to act as a base class to be inherited by other classes. Both Stat
ement are correct. For more information Refer:Abstract Classes in Java
Option (D) is correct.

Question 2

We can make a class abstract by

Declaring it abstract using the virtual keyword

Making at least one member function as virtual function

Making at least one member function as pure virtual function

Making all member function const

Discuss it

Question 2 ‒ Explanation
An abstract class is a class that is designed to be specifically used as
a base class. An abstract class contains at least one pure virtual funct
ion. A pure virtual function can be declared by using a pure specifier
( = 0 ) in the declaration of a virtual member function in the class de
claration. Option (C) is correct.

Question 3

Which of the following is used to make an Abstract class?

Making atleast one member function as pure virtual function

Making atleast one member function as virtual function

Declaring as Abstract class using virtual keyword

Declaring as Abstract class using static keyword

Discuss it

Question 3 ‒ Explanation
Making atleast one member function as pure virtual function is the met
hod to make abstract class. For more information on Abstract Class Ref
er:Pure Virtual Functions and Abstract Classes in C++ Option (A) is corre
ct.

Question 4

Type IV JDBC driver is a driver

which is written in C++

which requires an intermediate layer

which communicates through Java sockets

which translates JDBC function calls into API not native to DBMS

Discuss it

Question 4 ‒ Explanation
JDBC type 4 driver,works directly by connecting to the database serv
er through socket connections and converts JDBC calls to vendor-sp
ecific database protocols.These drivers don\'t require any intermedia
te layer. So, option (C) is correct.

Question 5
Predict the output of the following program.

Java

abstract class demo


{
public int a;
demo()
{
a = 10;
}

abstract public void set();

abstract final public void get();

class Test extends demo


{

public void set(int a)


{
this.a = a;
}

final public void get()


{
System.out.println("a = " + a);
}

public static void main(String[] args)


{
Test obj = new Test();
obj.set(20);
obj.get();
}
}

a = 10

a = 20

Compilation error

Discuss it

Question 5 ‒ Explanation
Final method can’t be overridden. Thus, an abstract function can’t b
e final.

Question 6

Which of the following is FALSE about abstract classes in Java


If we derive an abstract class and do not implement all the abstract
methods, then the derived class should also be marked as abstract
using \'abstract\' keyword

Abstract classes can have constructors

A class can be made abstract without any abstract method

A class can inherit from multiple abstract classes.

Discuss it

Question 7

Which of the following is true about interfaces in java.

1) An interface can contain following type of members.


....public, static, final fields (i.e., constants)
....default and static methods with bodies

2) An instance of interface can be created.

3) A class can implement multiple interfaces.

4) Many classes can implement the same interface.

1, 3 and 4

1, 2 and 4

2, 3 and 4

1, 2, 3 and 4

Discuss it

Question 7 ‒ Explanation
Answer: (A)

Explanation: The instance of an interface can't be created because it a


cts as an abstract class.

Quiz of this Question

You have completed 7/7 questions .


Your accuracy is 57%.

Last Updated : Mar 22, 2024


Take a part in the ongoing discussion View All Discussion

Company Languages DSA Data Web Python


About Us Python Data Science & Technologies Tutorial
Corporate & Legal Java Structures
Communications Address:- In Media
ML HTML Python
A-143, 9th Floor, Sovereign C++ Algorithms CSS Programming
Data Science
Corporate Tower, Sector- Contact Us PHP DSA for JavaScript Examples
With Python
136, Noida, Uttar Pradesh Advertise with GoLang Beginners TypeScript Python Projects
(201305) | Registered Data Science
us SQL Basic DSA ReactJS Python Tkinter
Address:- K 061, Tower K, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Gulshan Vivante Apartment, Solution
Machine
Sector 137, Noida, Gautam Android DSA Bootstrap OpenCV Tutorial
Learning
Buddh Nagar, Uttar Pradesh, Placement Tutorial Roadmap Web Design Python Interview
ML Maths
201305 Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Digital Logic Patterns Aptitude
Design OOAD Preparation
Engineering System Puzzles
Maths Design
Software Bootcamp
Development Interview
Software Questions
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved


Java Class and Object Trending in News V

105 Funny Things to Do to M


Question 1 Someone Laugh
Best PS5 SSDs in 2024: Top P
for Expanding Your Storage
Predict the output of following Java program?
Best Nintendo Switch Contr
in 2024
Java
#geekstreak2024 – 21 Days P
Challenge Powered By Deut
Bank
class Test {
int i; Full Stack Developer Roadm
} [2024 Updated]
class Main {
public static void main(String args[]) {
Test t;
System.out.println(t.i);
}

garbage value

compiler error

runtime error

Discuss it

Question 1 ‒ Explanation
t is just a reference, the object referred by t is not allocated any memory. Unlike C++, in Java all
non-primitive objects must be explicitly allocated and these objects are allocated on heap. The
following is corrected program.

Java

class Test {
int i;
}
class Main {
public static void main(String args[]) {
Test t = new Test();
System.out.println(t.i);
}

Question 2

JAVA
class demo
{
int a, b;

demo()
{
a = 10;
b = 20;
}

public void print()


{
System.out.println ("a = " + a + " b = " + b + "\\n");
}
}

class Test
{

public static void main(String[] args)


{
demo obj1 = new demo();
demo obj2 = obj1;

obj1.a += 1;
obj1.b += 1;

System.out.println ("values of obj1 : ");


obj1.print();
System.out.println ("values of obj2 : ");
obj2.print();

}
}

Compile error

values of obj1:
a = 11 b = 21
values of obj2:
a = 11 b = 21

values of obj1:
a = 11 b = 21
values of obj2:
a = 10 b = 20

values of obj1:
a = 11 b = 20
values of obj2:
a = 10 b = 21

Run time error

Discuss it

Question 2 ‒ Explanation
Assignment of obj2 to obj1 makes obj2 a reference to obj1. Therefore, any change in obj1 will
be reflected in obj2 also.

Question 3

Predict the output of following Java program.

Java
class demoClass
{
int a = 1;

void func()
{
demo obj = new demo();
obj.display();
}

class demo
{
int b = 2;

void display()
{
System.out.println("\\na = " + a);
}
}

void get()
{
System.out.println("\\nb = " + b);
}
}

class Test
{
public static void main(String[] args)
{
demoClass obj = new demoClass();
obj.func();
obj.get();

}
}

a = 1
b = 2

Compilation error

b = 2
a = 1

Discuss it

Question 3 ‒ Explanation

Members of inner class ‘demo’ can not be used in the outer class ‘Test’. Thus, get() of outer clas
s can not access variable ‘b’ of inner class.

Question 4

Predict the output of the following program.

Java

class First
{

void display()
{
System.out.println("Inside First");
}
}

class Second extends First


{

void display()
{
System.out.println("Inside Second");
}
}

class Test
{

public static void main(String[] args)


{
First obj1 = new First();
Second obj2 = new Second();

First ref;
ref = obj1;
ref.display();

ref = obj2;
ref.display();
}
}

Compilation error

Inside First
Inside Second

Inside First
Inside First

Runtime error

Discuss it

Question 4 ‒ Explanation
‘ref’ is a reference variable which obtains the reference of object of class First and calls its functi
on display(). Then ‘ref’ refers to object of class Second and calls its function display().

Question 5

Predict the output of the following program.

Java

class Test
{
int a = 1;
int b = 2;

Test func(Test obj)


{
Test obj3 = new Test();
obj3 = obj;
obj3.a = obj.a++ + ++obj.b;
obj.b = obj.b;
return obj3;
}
public static void main(String[] args)
{
Test obj1 = new Test();
Test obj2 = obj1.func(obj1);

System.out.println("obj1.a = " + obj1.a + " obj1.b = " + obj1.b);


System.out.println("obj2.a = " + obj2.a + " obj1.b = " + obj2.b);

}
}

obj1.a = 1 obj1.b = 2
obj2.a = 4 obj2.b = 3

obj1.a = 4 obj1.b = 3
obj2.a = 4 obj2.b = 3

Compilation error

Discuss it

Question 5 ‒ Explanation

obj1 and obj2 refer to same memory address.

Question 6

Predict the output of the following program.

Java

class Test
{
int a = 1;
int b = 2;

Test func(Test obj)


{
Test obj3 = new Test();
obj3 = obj;
obj3.a = obj.a++ + ++obj.b;
obj.b = obj.b;
return obj3;
}

public static void main(String[] args)


{
Test obj1 = new Test();
Test obj2 = obj1.func(obj1);

System.out.println("obj1.a = " + obj1.a + " obj1.b = " + obj1.b);


System.out.println("obj2.a = " + obj2.a + " obj1.b = " + obj2.b);

}
}

obj1.a = 1 obj1.b = 2
obj2.a = 4 obj2.b = 3

obj1.a = 4 obj1.b = 3
obj2.a = 4 obj2.b = 3

Compilation error
Discuss it

Question 6 ‒ Explanation

obj1 and obj2 refer to same memory address.

Question 7

What is the output of the following JAVA program? public class Good{ Private int m; Public Good(int
m){this.m=m;} public Boolean equals(Good n){return n.m=m;} public static void main(String args [ ]){
Good m1=new Good(22); Good m2=new Good(22); Object S1=new Good(22); Object S2=new
good(22); System.out.println(m1.equals(m2)); System.out.println(m1.equals(s2));
System.out.println(m1.equals(s2)); System.out.println(s1.equals(m2)); } }

True, True, False, False

True, False, True, False

True, True, False, True

True, False, False, False

None o the above.

Discuss it

Question 7 ‒ Explanation
Note: Question is wrong. First change boolean to int data type then there is a possibility of opti
on 4 is correct. So marks will be added to all students. GeeksforGeeks code link: https://ide.gee
ksforgeeks.org/tWbTXjT2VA Output: true false false false

Question 8

What is the output of the following JAVA program ?

class simple
{
public static void main(String[ ] args)
{
simple obj = new simple( );
obj.start( );
}
void start( )
{
long [ ] P= {3, 4, 5};
long [ ] Q= method (P);
System.out.print (P[0] + P[1] + P[2]+”:”);
System.out.print (Q[0] + Q[1] + Q[2]);
}
long [ ] method (long [ ] R)
{
R [1]=7;
return R;
}
} //end of class
12 : 15

15 : 12

12 : 12

15 : 15

Discuss it

Question 8 ‒ Explanation
When above program compliled and run on ide then it will produce 15:15. IDE link Option (D) is cor
rect.

Question 9

What is the output of the following JAVA program ?

Java

Class Test {
public static void main(String[] args) {
Test obj = new Test();
obj.start();
}
void start() {
String stra = ”do”;
String strb = method(stra);
System.out.print(“: ”+stra + strb);
}
String method(String stra) {
stra = stra + ”good”;
System.out.print(stra);
return“ good”;
}
}

dogood : dogoodgood

dogood : gooddogood

dogood : dodogood

dogood : dogood

Discuss it

Question 10

Java uses threads to enable the entire environment to be ______.

Symmetric

Asymmetric

Synchronous
Asynchronous

Discuss it

Question 10 ‒ Explanation
Java uses threads to enable the entire environment to be asynchronous. Asynchronous threadi
ng is pre-emptive i.e. a thread once start executing a task it can hold it in mid, save the current
state and start executing another task (context switching) according to priority and other specif
ied criteria and threading. So, option (D) is correct.

1 2

You have completed 10/12 questions .


Your accuracy is 70%.

Last Updated : Mar 22, 2024

Take a part in the ongoing discussion View All Discussion

Company Languages DSA Data Web Python


About Us Python Data Science & Technologies Tutorial
Corporate & Communications Legal Java Structures
Address:- A-143, 9th Floor, In Media
ML HTML Python
C++ Algorithms CSS Programming
Sovereign Corporate Tower, Data Science
Sector- 136, Noida, Uttar Pradesh Contact Us PHP DSA for JavaScript Examples
With Python
(201305) | Registered Address:- K Advertise with GoLang Beginners TypeScript Python Projects
061, Tower K, Gulshan Vivante Data Science
us SQL Basic DSA ReactJS Python Tkinter
Apartment, Sector 137, Noida, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Gautam Buddh Nagar, Uttar Solution
Machine
Pradesh, 201305 Android DSA Bootstrap OpenCV Tutorial
Learning
Placement Tutorial Roadmap Web Design Python Interview
ML Maths
Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Patterns
Digital Logic OOAD Aptitude
Design System Preparation
Engineering Design Puzzles
Maths Bootcamp
Software Interview
Development Questions
Software
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved


DSA
Data Science Sign In
Web Tech
Courses
Trending Now Data Structures Algorithms System Design Foundational Courses Data Science Practice Problem Python Machine Le

Java Class and Object Trending in News View More

105 Funny Things to Do to Make


Question 11 Someone Laugh
Best PS5 SSDs in 2024: Top Picks for
Expanding Your Storage
In Java, when we implement an interface method, it must be declared as:
Best Nintendo Switch Controllers in
2024
Private
#geekstreak2024 – 21 Days POTD
Protected Challenge Powered By Deutsche Bank
Full Stack Developer Roadmap [2024
Public Updated]

Friend

Discuss it

Question 11 ‒ Explanation
In Java, when we implement an interface method, it must be declared
as Public. For more information on Java interface Refer:Interfaces in Ja
va option (C) is correct.

Question 12

Predict the output of following Java program

Java

class Test {
int i;
}
class Main {
public static void main(String args[]) {
Test t = new Test();
System.out.println(t.i);
}
}

garbage value

compiler error

runtime error
Discuss it

Question 12 ‒ Explanation
In Java, fields of classes and objects that do not have an explicit initi
alizer and elements of arrays are automatically initialized with the de
fault value for their type (false for boolean, 0 for all numerical types,
null for all reference types). Local variables in Java must be definitely
assigned to before they are accessed, or it is a compile error.

1 2

You have completed 12/12 questions .


Your accuracy is 75%.

Last Updated : Mar 22, 2024

Take a part in the ongoing discussion View All Discussion

Company Languages DSA Data Web Python


About Us Python Data Science & Technologies Tutorial
Corporate & Legal Java Structures
Communications Address:- In Media
ML HTML Python
C++ Algorithms CSS Programming
A-143, 9th Floor, Sovereign Data Science
Corporate Tower, Sector- Contact Us PHP DSA for JavaScript Examples
With Python
136, Noida, Uttar Pradesh Advertise with GoLang Beginners TypeScript Python Projects
(201305) | Registered Data Science
us SQL Basic DSA ReactJS Python Tkinter
Address:- K 061, Tower K, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Gulshan Vivante Apartment, Solution
Machine
Sector 137, Noida, Gautam Android DSA Bootstrap OpenCV Tutorial
Learning
Buddh Nagar, Uttar Pradesh, Placement Tutorial Roadmap Web Design Python Interview
ML Maths
201305 Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Digital Logic Patterns Aptitude
Design OOAD Preparation
Engineering System Puzzles
Maths Design
Software Bootcamp
Development Interview
Software Questions
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved


Java final keyword

Question 1

What is the use of final keyword in Java?

When a class is made final, a subclass of it can not be created.

When a method is final, it can not be overridden.

When a variable is final, it can be assigned value only once.

All of the above

Discuss it

Question 1 ‒ Explanation
See final in Java.

Question 2

Output of following Java program

Java
Trending Now Data Structures Algorithms System Design Foundational Courses Data Science Practice Problem Python Machine L

class Main {
public static void main(String args[]){
final int i;
i = 20;
System.out.println(i);
}
}

20

Compiler Error

Garbage value

Discuss it

Question 2 ‒ Explanation
There is no error in the program. final variables can be assigned value only once. In the above program, i is ass
igned a value as 20, so 20 is printed.

Question 3

Java

class Main {
public static void main(String args[]){
final int i;
i = 20;
i = 30;
System.out.println(i);
}
}

30

Compiler Error

Garbage value

Discuss it

Question 3 ‒ Explanation
i is assigned a value twice. Final variables can be assigned values only one. Following is the compiler error "Mai
n.java:5: error: variable i might already have been assigned"

Question 4

Java

class Base {
public final void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived::show() called");
}
}
public class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}

Derived::show() called

Base::show() called

Compiler Error

Exception

Discuss it

Question 4 ‒ Explanation
compiler error: show() in Derived cannot override show() in Base

You have completed 4/4 questions .


Your accuracy is 100%.

Last Updated : Mar 22, 2024

Take a part in the ongoing discussion View All Discussion

Company Languages DSA Data Web Python


About Us Python Data Science & Technologies Tutorial
Corporate & Legal Java Structures
Communications Address:- In Media
ML HTML Python
C++ Algorithms CSS Programming
A-143, 9th Floor, Sovereign Data Science
Corporate Tower, Sector- Contact Us PHP DSA for JavaScript Examples
With Python
136, Noida, Uttar Pradesh Advertise with GoLang Beginners TypeScript Python Projects
(201305) | Registered Data Science
us SQL Basic DSA ReactJS Python Tkinter
Address:- K 061, Tower K, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Gulshan Vivante Apartment, Solution
Machine
Sector 137, Noida, Gautam Android DSA Bootstrap OpenCV Tutorial
Learning
Buddh Nagar, Uttar Pradesh, Placement Tutorial Roadmap Web Design Python Interview
ML Maths
201305 Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Digital Logic Patterns Aptitude
Design OOAD Preparation
Engineering System Puzzles
Maths Design
Software Bootcamp
Development Interview
Software Questions
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved


DSA
Data Science Sign In
Web Tech
Courses
Trending Now Data Structures Algorithms System Design Foundational Courses Data Science Practice Problem Python Machine L

Java Data Types Trending in News View More

105 Funny Things to Do to Make


Question 1 Someone Laugh
Best PS5 SSDs in 2024: Top Picks
for Expanding Your Storage
Best Nintendo Switch Controllers
C in 2024
#geekstreak2024 – 21 Days POTD
class Main {
Challenge Powered By Deutsche
public static void main(String args[]) { Bank
int t; Full Stack Developer Roadmap
System.out.println(t);
[2024 Updated]
}
}

garbage value

compiler error

runtime error

Discuss it

Question 1 ‒ Explanation
Unlike class members, local variables of methods must be assigne
d a value to before they are accessed, or it is a compile error.

Question 2

Predict the output of following Java program.

class Test {
public static void main(String[] args) {
for(int i = 0; 0; i++)
{
System.out.println("Hello");
break;
}
}
}
Hello

Empty Output

Compiler error

Runtime error

Discuss it

Question 2 ‒ Explanation
The error is in for loop where 0 is used in place of boolean value.
Unlike C++, use of non boolean variables in place of bool is not all
owed

Question 3

Predict the output of the following program.

Java

class Test
{
public static void main(String[] args)
{
Double object = new Double("2.4");
int a = object.intValue();
byte b = object.byteValue();
float d = object.floatValue();
double c = object.doubleValue();

System.out.println(a + b + c + d );

}
}

8.8

8.800000095367432

Discuss it

Question 3 ‒ Explanation
Arithmetic conversions are implicitly performed to cast the values
to a common type. The compiler first performs integer promotion.
If the operands still have different types, then they are converted t
o the type that appears highest in the hierarchy.
Question 4

Which of the following statements is/are TRUE regarding JAVA ? (a)


Constants that cannot be changed are declared using the ‘static’
keyword. (b) A class can only inherit one class but can implement
multiple interfaces.

Only (a) is TRUE.

Only (b) is TRUE.

Both (a) and (b) are TRUE.

Neither (a) nor (b) are TRUE.

Discuss it

Question 4 ‒ Explanation
In JAVA, constant are not declared using \'static\' keyword and a cl
ass can implement multiple interfaces but class can inherit one cla
ss only. So, option (B) is correct.

You have completed 4/4 questions .


Your accuracy is 25%.

Last Updated : Mar 22, 2024

Take a part in the ongoing discussion View All Discussion


Company Languages DSA Data Web Python
About Us Python Data Science & Technologies Tutorial
Corporate & Legal Java Structures
Communications Address:- In Media
ML HTML Python
C++ Algorithms CSS Programming
A-143, 9th Floor, Sovereign Data Science
Corporate Tower, Sector- Contact Us PHP DSA for JavaScript Examples
With Python
136, Noida, Uttar Pradesh Advertise with GoLang Beginners TypeScript Python Projects
(201305) | Registered Data Science
us SQL Basic DSA ReactJS Python Tkinter
Address:- K 061, Tower K, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Gulshan Vivante Apartment, Solution
Machine
Sector 137, Noida, Gautam Android DSA Bootstrap OpenCV Tutorial
Learning
Buddh Nagar, Uttar Pradesh, Placement Tutorial Roadmap Web Design Python Interview
ML Maths
201305 Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Digital Logic Patterns Aptitude
Design OOAD Preparation
Engineering System Puzzles
Maths Design
Software Bootcamp
Development Interview
Software Questions
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved


Java Arrays Trending in News V

105 Funny Things to Do to M


Question 1 Someone Laugh
Best PS5 SSDs in 2024: Top P
for Expanding Your Storage
Best Nintendo Switch Contr
C in 2024
#geekstreak2024 – 21 Days P
class Test {
Challenge Powered By Deut
public static void main(String args[]) { Bank
int arr[2]; Full Stack Developer Roadm
System.out.println(arr[0]);
[2024 Updated]
System.out.println(arr[1]);
}
}

0
0

garbage value
garbage value

Compiler Error

Exception

Discuss it

Question 1 ‒ Explanation
In Java, it is not allowed to put the size of the array in the declaration because an array declarat
ion specifies only the element type and the variable name. The size is specified when you alloca
te space for the array. Even the following simple program won\'t compile.

class Test {
public static void main(String args[]) {
int arr[5]; //Error
}
}

Question 2

Output of following Java program?

Java

import java.util.Arrays;
class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (Arrays.equals(arr1, arr2))
System.out.println("Same");
else
System.out.println("Not same");
}
}

Same

Not Same

Discuss it

Question 2 ‒ Explanation
See http://www.geeksforgeeks.org/compare-two-arrays-java/

Question 3

Consider the following C program which is supposed to compute the transpose of a given 4 x 4
matrix M. Note that, there is an X in the program which indicates some missing statements. Choose
the correct option to replace X in the program.

#include<stdio.h>
#define ROW 4
#define COL 4
int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main()
{
int i, j, t;
for (i = 0; i < 4; ++i)
{
X
}
for (1 = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
printf ("%d", M[i][j]);
}

for(j = 0; j < 4; ++j){


t = M[i][j];
M[i][j] = M[j][i];
A) M[j][i] = t;
}

for(j = 0; j < 4; ++j){


M[i][j] = t;
t = M[j][i];
B) M[j][i] = M[i][j];
}

C) for(j = i; j < 4; ++j){


t = M[i][j];
M[i][j] = M[j][i];
M[j][i] = t;
}

for(j = i; j < 4; ++j){


M[i][j] = t;
t = M[j][i];
D) M[j][i] = M[i][j];
}

Discuss it

Question 3 ‒ Explanation
To compute transpose j needs to be started with i,so A and B are WRONG
In D, given statement is wrong as temporary variable t needs to be assigned some value and N
OT vice versa

M[i][j] = t;

So the answer is C Check out the correct option C at Solution: http://code.geeksforgeeks.org/r


7wbP6

Question 4

Output of following Java program?

class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not same");
}
}

Same

Not Same

Discuss it

Question 4 ‒ Explanation
See http://www.geeksforgeeks.org/compare-two-arrays-java/

Question 5

Java

class Test {
public static void main(String args[]) {
int arr[] = new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}

0
0

garbage value
garbage value

Compiler Error

Exception

Discuss it

Question 5 ‒ Explanation
Java arrays are first class objects and all members of objects are initialized with default values li
ke o, null.

Question 6

Java

class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1.equals(arr2))
System.out.println("Same");
else
System.out.println("Not same");
}
}

Same

Not same
Discuss it

Question 6 ‒ Explanation
arr1.equals(arr2) is same as (arr1 == arr2)

Question 7

Predict the output?

Java

// file name: Main.java


public class Main {
public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i < arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
}

10 20 30 40 50

Compiler Error

10 20 30 40

Discuss it

Question 7 ‒ Explanation
It is a simple program where an array is first created then traversed. The important thing to not
e is, unlike C++, arrays are first class objects in Java. For example, in the following program, siz
e of array is accessed using length which is a member of arr[] object.

Question 8

Which of the following is FALSE about arrays in Java?

A java array is always an object

Length of array can be changed after creation of array

Arrays in Java are always allocated on heap

Discuss it

Question 8 ‒ Explanation
In Java, arrays are objects, they have members like length. The length member is final and cannot
be changed. All objects are allocated on heap in Java, so arrays are also allocated on heap.
Question 9

public class Main {


public static void main(String args[]) {
int arr[][] = new int[4][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
arr[3] = new int[4];

int i, j, k = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
arr[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
System.out.print(" " + arr[i][j]);
k++;
}
System.out.println();
}
}
}

Compiler Error

0
1 2
3 4 5
6 7 8 9

0
0 0
0 0 0
0 0 0 0

9
7 8
4 5 6
0 1 2 3

Discuss it

Question 9 ‒ Explanation
In Java, we can create jagged arrays. Refer Jagged Array in Java for details.

You have completed 9/9 questions .


Your accuracy is 44%.

Last Updated : Mar 22, 2024


Take a part in the ongoing discussion View All Discussion

Company Languages DSA Data Web Python


About Us Python Data Science & Technologies Tutorial
Corporate & Communications Legal Java Structures
Address:- A-143, 9th Floor, In Media
ML HTML Python
C++ Algorithms CSS Programming
Sovereign Corporate Tower, Data Science
Sector- 136, Noida, Uttar Pradesh Contact Us PHP DSA for JavaScript Examples
With Python
(201305) | Registered Address:- K Advertise with GoLang Beginners TypeScript Python Projects
061, Tower K, Gulshan Vivante Data Science
us SQL Basic DSA ReactJS Python Tkinter
Apartment, Sector 137, Noida, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Gautam Buddh Nagar, Uttar Solution
Machine
Pradesh, 201305 Android DSA Bootstrap OpenCV Tutorial
Learning
Placement Tutorial Roadmap Web Design Python Interview
ML Maths
Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Digital Logic Patterns Aptitude
Design OOAD Preparation
Engineering System Puzzles
Maths Design
Software Bootcamp
Development Interview
Software Questions
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved


Tutorials
DSA
Data Science Sign In
Web Tech
Courses
Trending Now Data Structures Algorithms System Design Foundational Courses Data Science Practice Problem Python Machine Learning Dat

You might also like