Practice Questions for Java

  • Why Java is a Popular Programming Language?
Answer: Java is an object-oriented programming (OOP) language. OOP offers a neat modular structure, thereby making it easier to solve complex problems. Its modular nature helps programmers to write reusable code and also enhances the development process. Its scalability, stability, and versatility make it one of the highly popular programming languages. It can be used on devices ranging from mobile phones and desktop computers to large-scale industry servers and applications. It is gradually gaining credibility in areas like Cloud development and IoT. Therefore, it is one of the top programming languages for businesses. For long, Java has been used for developing Web apps, Android apps, and software tools like Eclipse, IntelliJ IDEA, NetBeans IDE, etc. Today, Java’s use cases have expanded to Data Science applications, Machine Learning applications, and even IoT. Developers and programmers prefer Java for designing all such applications because Java has a suite of high-level concurrency tools and packages that make it possible to create highly scalable solutions. Also, Java is multi-platform compatible.
  • Why Java is a Platform Independent Language?
Answer: Java is platform-independent because it uses a virtual machine. The Java programming language and all APIs are compiled into bytecode. Bytecode is effectively platform-independent. The virtual machine takes care of the differences between the bytecodes for the different platforms. The run-time requirements for Java are therefore very small. The Java virtual machine takes care of all hardware-related issues so that no code has to be compiled for different hardware.The byte code generated by source code compilation would run in any operating system, but the JVM present in a machine differs for each operating system. And this is how java is considered a platform-independent programming language.

  • What is Java Development Kit(JDK), What all components are available in JDK.
Answer: The Java Development Kit (JDK) is a software development environment used to develop Java applications and applets. It contains JRE and several development tools, an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (javadoc) accompanied with another tool.
Java Runtime Environment: The JRE software builds a runtime environment in which Java programs can be executed. The JRE is the on-disk system that takes your Java code, combines it with the needed libraries, and starts the JVM to execute it. The JRE contains libraries and software needed by your Java programs to run.

  • What is Java Virtual Machine?
Answer: Java applications are called WORA(Write once Run Anywhere) because of their ability to run a code on any platform. This is done only because of JVM. The JVM is a Java platform component that provides an environment for executing Java programs. JVM interprets the bytecode into machine code which is executed in the machine in which the Java program runs.

JVM performs the following functions: 
  • Loads the code
  • Verifies the code
  • Executes the code
  • Provides runtime environment
Class Loader: Class loader is a subsystem of JVM. It is used to load class files. Whenever we run the java program, class loader loads it first.

Class method area: It is one of the Data Area in JVM, in which Class data will be stored. Static Variables, Static Blocks, Static Methods, Instance Methods are stored in this area.

Heap: A heap is created when the JVM starts up. It may increase or decrease in size while the application runs.

Stack: JVM stack is known as a thread stack. It is a data area in the JVM memory which is created for a single execution thread. The JVM stack of a thread is used by the thread to store various elements i.e.; local variables, partial results, and data for calling method and returns.

Native stack: It subsumes all the native methods used in your application.

JIT compiler: The Just-In-Time (JIT) compiler is a part of the runtime environment. It helps in improving the performance of Java applications by compiling bytecodes to machine code at run time. The JIT compiler is enabled by default. When a method is compiled, the JVM calls the compiled code of that method directly. The JIT compiler compiles the bytecode of that method into machine code, compiling it “just in time” to run.

Garbage collector: As the name explains that Garbage Collector means to collect the unused material. Well, in JVM this work is done by Garbage collection. It tracks each and every object available in the JVM heap space and removes unwanted ones.
Garbage collector works in two simple steps known as Mark and Sweep:
  • Differentiate between compiler and interpreter in context of java?
Answer: Differences between an interpreter and a compiler. 
    • The interpreter scans the program line by line and translates it into machine code whereas the compiler scans the entire program first and then translates it into machine code.
    • The interpreter shows one error at a time whereas the compiler shows all errors and warnings at the same time.
    • In the interpreter, the error occurs after scanning each line whereas, in the compiler, the error occurs after scanning the whole program.
    • In an interpreter, debugging is faster whereas, in the compiler, debugging is slow.
    • Execution time is more in the interpreter whereas execution time is less in the compiler.
    • An interpreter is used by languages such as Java, and Python, and the compiler is used by languages such as C, C++, etc.
  • What is an Interpreter in Java?
AnswerRoles of Java Interpreter
    • To convert the bytecode into the native code of the machine.
    • This process is done line by line.
    • If the error comes on any line, the process is stopped over there.
  • What is Bytecode in Java and how it works?
Answer: Byte Code can be defined as an intermediate code generated by the compiler after the compilation of source code(JAVA Program). This intermediate code makes Java a platform-independent language.
Compiler converts the source code or the Java program into the Byte Code(or machine code), and secondly, the Interpreter executes the byte code on the system. The Interpreter can also be called JVM(Java Virtual Machine). The byte code is the common piece between the compiler(which creates it) and the Interpreter (which runs it).
  • Why Java is a Secure Language and explain sandbox model?
Answer: Java is the most popular object-oriented programming language. It provides a variety of salient features that are preferred by the developers. It is the reason that a billion of devices runs on Java. In this section, we are going to discuss why Java is secure.
Java is secure due to the following reasons:
    • Java programs run inside a virtual machine which is known as a sandbox.
    • Java does not support explicit pointer.
    • Byte-code verifier checks the code fragments for illegal code that can violate access right to object.
    • It provides java.security package implements explicit security.
    • It provides library level safety.
A sandbox is an isolated computing environment in which a program or file can be executed without affecting the application in which it runs. A sandbox typically provides a tightly controlled set of resources for guest programs to run in, such as limited space on disk and memory. In a Java programming language, the sandbox is the program area and it has some set of rules that programmers need to follow when creating Java code (like an applet) that is sent as part of a page.
  • What are the different Applications of Java?
  • What are Java Keywords and reserved words?
  • What is a Constructor in Java and explain various types of constructors in java?
  • What is garbage collection in Java?
  • What are Operators in Java and its Types?
  • Explain primitive data types in Java language?
  • Can we overload main function in java?
  • What is Conditional Operators in Java and how to write it?
  • What is a final keyword in Java and Explain its various uses with example?
  • What is for loop in java and how to implement it?
  • What is a While Loop in Java and how to use it?
  • What is for-each loop in Java?
  • What is a Do while loop in Java and how to use it?
  • What is a Switch Case In Java?
  • What is a Scanner Class in Java?
  • What is the default value of Char in Java?
  • What is the use of this Keyword In Java?
  • What is protected keyword in Java and How to Implement it?
  • What is a static keyword in Java?
  • Why is the main method static in java?
  • What is a anonymous inner Class? 
  • What is an anonymous object or nameless object?
  • What is an Array class in Java and How to Implement it?
  • What is ternary operator in Java and how can you use it?
  • What is Modulus in Java and how does it work?
  • What is the difference between Method Overloading And Overriding?
  • What is typecasting in Java and how does it work?
  • How to use substring() Method in java?
  • Why String is immutable in java?
  • What are the differences between String, StringBuffer and StringBuilder?
  • How to split a String in Java?
  • What is Coupling in Java and its different types?
  • What is Trim method in Java and How to Implement it?
  • How to Implement Method Hiding in Java
  • How to Create and Use Packages in Java?
  • What are the various access specifiers in Java explain their use in context of inheritance?
  • How To implement Inner Class In Java?
  • How to implement Nested Class in Java?
  • How To implement Multiple Inheritance In Java?
  • What is the use of Abstract Method in Java?
  • What is association in Java and why do you need it?
  • What is aggregation in Java and why do you need it?
  • What is the difference between Abstract Class and Interface in Java?
  • How To Implement Volatile Keyword in Java?
  • What is Math Class in Java and How to use it?
  • How to Generate Random Numbers using Random Class in Java?
  • What is use of transient keyword in Java?
  • How To Implement Static Block In Java?
  • What is power function in Java? 
  • How to Convert Int to String in Java?
  • How to get Date and Time in Java?
  • What is Trim method in Java and How to Implement it?
  • What is AutoBoxing and unboxing in Java?
  • What is Singleton Class in java and how to use it?
  • What is Integer class in java and how it works?
  • What is System Class in Java and how to implement it?
  • What is Character class in Java?
  • What is the Boolean Class in Java and how to use it?
  • What is Dynamic Binding In Java And How To Use It?
  • What is the Difference Between Extends and Implements in Java?
  • What is the difference between checked and unchecked exceptions in java?
  • What is exception handling in java?
  • Explain the use and role of try, catch, finally, throw, throws?
  • What is Collection Interface? 
  • What is Vector class in Java and how to use it?
  • What is Stack Class in Java and how to use it?
  • What is a Hashtable class in Java and how to use it? 
  • What is an enumeration interface in Java and how to use it?
  • What is a set interface in Java and how to use it? 
  • What is a List interface in Java and how to use it? 
  • What is a Map interface in Java and how to use it?
  • What is an Iterator interface in Java and how to use it?
  • Write a java program to find the first non-repeating character in a given String?
  • Write a java program to read a matrix and find the biggest and smallest value and interchange their position. 
  • Write a java program to find the diagonal sum of the matrix. 

No comments:

Post a Comment