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

Quiz About Java Final Keyword

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)
18 views

Quiz About Java Final Keyword

Uploaded by

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

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

You might also like