Sunday, April 14, 2024

Interview Questions In Java Programming

Don't Miss

Can We Override The Private Methods

Java Interview Questions And Answers | Java Programming Interview Questions And Answers |Simplilearn

No, we cannot override the private methods because the scope of private methods is limited to the class and we cannot access them outside of the class.

84) Can we change the scope of the overridden method in the subclass?

Yes, we can change the scope of the overridden method in the subclass. However, we must notice that we cannot decrease the accessibility of the method. The following point must be taken care of while changing the accessibility of the method.

What Are The Different Types Of Waits Available In Webdriver

There are two types of waits available in WebDriver:

  • Implicit wait: These waits are used to provide a default waiting time between the consecutive test steps across the entire test script. Hence, the subsequent test step would only be executed when the 30 seconds are over after executing the previous test step.
  • Explicit wait: These waits are used to halt the execution until a particular condition is met or the maximum time has elapsed.

Explicit waits are instantiated for a particular instance only, whereas implicit waits are not.

Want to learn about Selenium with Python! Check out our blog on Selenium Python Tutorial for beginners.

What Will Be The Output Of The Below Java Program And Define The Steps Of Execution Of The Java Program With The Help Of The Below Code

classInterviewBitstatic    static    InterviewBitpublicstaticvoidmainpublicvoidmethod_1static    publicstaticvoidmethod_2}

The Output we get by executing this program will be

Static Block 1. Value of j = 0Static method.

  • Instance block 1.
  • Instance method_1.

Like a static variable, the instance variable also has been initialized with the default value 0 and will be in the state of reading and writing indirectly.

5. It will execute the instance methods and assign the original value to the instance variable.

  • Prints the Instance block 1. And the current value of i is not assigned till now, so it will print 0.
  • Assign the original value to i. Then print instance block 2. And after that instance method will be called and printed because it is being called in the instance block.

6. And at the last step, the constructor will be invoked and the lines will be executed in the constructor.

This is how the java program gets executed.

Read Also: Stay Interviews To Improve Retention

What Is The Difference Between The Throw And Throws Keyword In Java

  • The throw keyword is used to manually throw the exception to the calling method.
  • And the throws keyword is used in the function definition to inform the calling method that this method throws the exception. So if you are calling, then you have to handle the exception.

Example –

classMainpublicstaticvoidmaincatch   }}

Here in the above snippet, the method testExceptionDivide throws an exception. So if the main method is calling it then it must have handled the exception. Otherwise, the main method can also throw the exception to JVM.

And the method testExceptionDivide ‘throws the exception based on the condition.

What Is The Difference Between Array List And Vector In Java

Commonly Asked Java Programming Interview Questions
If an element is inserted into the Array List, it increases its Array size by 50%. Vector defaults to doubling size of its array.
Array List can only use Iterator for traversing an Array List. Vector can use both Enumeration and Iterator for traversing.
Array List is fast as its non-synchronized. Vector is slow as it is thread safe.

Also Check: What Is A Phone Screen Interview

Do Final Finally And Finalize Keywords Have The Same Function

All three keywords have their own utility while programming.

Final: If any restriction is required for classes, variables, or methods, the final keyword comes in handy. Inheritance of a final class and overriding of a final method is restricted by the use of the final keyword. The variable value becomes fixed after incorporating the final keyword. Example:

finalint a=100 a = 0   // error

The second statement will throw an error.

Finally: It is the block present in a program where all the codes written inside it get executed irrespective of handling of exceptions. Example:

try catch  finally 

Finalize: Prior to the garbage collection of an object, the finalize method is called so that the clean-up activity is implemented. Example:


Q47 Can We Have Two Methods In A Class With The Same Name

Ans: We can define two methods in a class with the same name but with different number/type of parameters. Which method is to get invoked will depend upon the parameters passed.

For example in the class below we have two print methods with same name but different parameters. Depending upon the parameters, appropriate one will be called:

public class methodExample     public void print     public static void main }

Don’t Miss: Questions To Ask Therapist Interview

It Is Not Necessary That You Print The Same Order Of Elements As Shown Above You Can Print Any Other Order But The Shape Of The Graph Of Elements Of The Array Should Look Like A Wave The Graph Should Always Start With The Peak And Not A Valley You Are Not Allowed To Use Any Extra Space And You Have To Solve This Problem In O Time Complexity

One basic approach to solve this problem is to sort the array and then swap the adjacent elements. The time complexity for which is O. Since we have to solve the problem in O time complexity, we can solve it using the Wave Sort algorithm.

Our aim is to generate the Wave Graph. The aim can be accomplished by aiming at generating the peaks in the array or aiming at generating the valleys in the array. So, let us try to generate peaks in the array. Since we want the first array to be the peak, we will leave it as it is and start from the index = 2. Here, since we want to generate a peak, we need to have the previous and next elements smaller than our current elements. So, we will check that if our previous element is larger than our element, we will swap them. Again, at the same position, we will also check that the next element should be smaller than our current element. If it is not, swap these 2 elements. This is shown below.

So, we have to take a jump of 2 indices every time till we reach the end of the array. Hence, we will be able to wave sort the array in O time and O space.

Java Code for Wave Sort

import java.util.* classMainpublicstaticvoidwaveSortif }}publicstaticvoidmainwaveSort System.out.println for }}

Sample Output

Input:79863472Output:After wave sort9683724

What Are Virtual Functions

Java 8 coding/programming Interview questions for freshers and Experienced | Code Decode | Examples

A virtual function or virtual method in an OOP language is a function or method used to override the behavior of the function inan inherited class with the same signature to achieve the polymorphism.

The virtual keyword is not used in Java to define the virtual function instead, the virtual functions and methods are achieved using the following techniques:

We can override the virtual function with the inheriting class function using the same function name. Generally, the virtual function isdefined in the parent class and override it in the inherited class.

A virtual function should have the same name and parameters in the base and derived class.

For the virtual function, an IS-A relationship is necessary, which is used to define the class hierarchy in inheritance.

The Virtual function cannot be private, as the private functions cannot be overridden.

The virtual functions can be used to achieve oops concepts like runtime polymorphism.

In a nutshell, the methods or functions that can be used to achieve the polymorphism are the virtual functions or methods in Java.

Recommended Reading: Formal Attire For An Interview

Explain Some Of The Intermediate Operations Which Are Used Commonly In The Java Programming Language

Some of the Intermediate operations that are used commonly in the Java are:

Stream.filter: The Java Stream filter method is one of the significant operations under intermediate operations. The filter method accepts a Predicate to separate all components of the stream. It is under intermediate operations because it enables the users to call another stream operation method) on the result. The predicate should declare true if the component is to be introduced in the final Stream, and the Predicate will reflect false if the component should not be included in the final stream. The intermediate operation, with the help of the given function, transforms each component in the stream into some other object. In simple words, the operation converts the element of a stream into something else. It takes the given function and applies that function to every element of the stream, which at the end returns a stream of the values that are produced by the parameter function. also supports the user to create a calculation on the data inside a Java stream. For example, if a user has a list of strings, the user can transform each string from the given list to uppercase, lowercase, or to a substring of the original string and something entirely else.

The Output Of The Above Code Will Be


8/16/22, 9:26 AM 300 Core Java Interview Questions – javatpoint


McAfee® Antivirus McAfee Official Store

In the first case, 10 and 20 are treated as numbers and added to be 30. Now, their sum 30 is treated as the string and concatenated with the string Javatpoint. Therefore, the output will be 30Javatpoint.

In the second case, the string Javatpoint is concatenated with 10 to be the string Javatpoint10 which will then be concatenated with 20 to be Javatpoint1020.

You May Like: Big Data Analytics Interview Questions

Question 3: Largest Sum Contiguous Subarray

Largest sum contiguous subarray is the task of finding the contiguous subarray within a one-dimensional array of numbers which has the largest sum.For example :

3 forthe sequence of values2,1,3,4,1,2,1,5,4 the contiguous subarray with the largest sum is4,1,2,1,with sum6

Solution :

What Are The Java Lambda Expressions

Top 30 Programming questions asked in Interview

Java lambda expressions were Java’s initial step towards functional programming. Java Lambda Expression or Lambda function is simply an unnamed expression or function that is rewritten as a parameter for any other function. Lambda Expressions in Java are the functions that are used to be shared as an object. It can also be used for an object to be referenced. Lambda Expressions need more concise coding, include and it also implements a method of executing the functional interfaces of Java 8. Lambda expressions in Java allow the users to express one functioning unit to carry throughout to different code.

Lambda Expressions Syntax:

Collection API is open to use since Java SE 1.2. Streams API were included in Java SE 8. 2. The collections framework is used to store data. In simple words, it is used to store a set of objects. Streams are used in computing data. In simple words, it is used in the computation of a set of objects. 3. In Collections API, the iteration of elements can be done with the help of both the Spliterator and Iterator. The forEach method can also be used by the user to perform a method on every element which is in the stream. In the Streams API, the users are not allowed to iterate the elements of the stream using the Spliterator and Iterator. 4. It is used to save an infinite quantity of elements. If the users want to do the processing on the elements of the Collections, then at that moment, Streams API is used.

Don’t Miss: How To Interview A Programmer

Can This Keyword Be Used To Refer Static Members

Yes, It is possible to use this keyword to refer static members because this is just a reference variable which refers to the current class object. However, as we know that, it is unnecessary to access static variables through objects, therefore, it is not the best practice to use this to refer static members. Consider the following example.



What Are The Thread States

Following are the different thread states:

New: A thread which is just instantiated is in the new state. When a start method is invoked, the thread becomes the ready state. Then it is moved to the runnable state by the thread scheduler.

Runnable: A thread which is ready to run

Running: A thread which is executing is in running state.

Blocked: A blocked thread is waiting for a monitor lock is in this state. This thing can also happen when a thread performs an I/O operation and moves to the next state.

Waiting: It is a thread that is waiting for another thread to do the specific action.

Timed_waiting: It is a thread that is waiting for another thread to perform.

Terminated: A thread that has exited is in this state.

Recommended Reading: How To Practice Lip Sync For Interview

Can The Static Methods Be Overridden

  • No! Declaration of static methods having the same signature can be done in the subclass but run time polymorphism can not take place in such cases.
  • Overriding or dynamic polymorphism occurs during the runtime, but the static methods are loaded and looked up at the compile time statically. Hence, these methods cant be overridden.

Explain Nashorn Javascript Engine In Java Se 8

Top 10 Java Interview Questions | Java Interview Questions And Answers | Simplilearn

Nashorn is a name given to a JavaScript engine that is included in JDK SE 8. With the guidance of Nashorn, it became easier for the user to produce the JavaScript code at Java virtual machine . Nashorn is included in the latest version of Java programming language that is Java SE 8 in order to help in restoring the existing JavaScript engine which is commonly known as Rhino. Nashorn is considerably more reliable than Rhino in phases of production.

The use of summoning dynamic characteristics, regeneration of the code of JavaScript directly into the bytecode straight inside the storage, etc., performs the Nashorn extra prominent in JDK 8. The user can accomplish the code of JavaScript by practicing the command-line tool plus by installing that code of JavaScript into the source code of Java.

Recommended Reading: How To Transcribe Interviews For Research

What Are Your Favorite Java Features

Some of the Java features that applicants may mention when responding to this interview question include:

  • Javas Secure feature to guarantee a virus-free system
  • The fact that Java is an object-oriented programming language
  • The fact that Java is an independent platform, and its virtual machine interprets its code

What Are The Testing Types That Are Supported By Selenium

Regression Testing and Functional Testing are majorly supported by Selenium.

  • Regression Testing:
  • All the Test cases are executed repeatedly either partially or as a full to ensure that the existing functionalities are working as designed. It includes the following steps,

    • Re-testing: All the test cases in the Test Suite are executed which proves to be time-consuming and expensive.
    • Regression Test case Selection: The test cases are classified or categorized as features tests, integration tests etc., and some test cases are selected.
    • Prioritization of Test cases: The test cases that are selected are prioritized based on the critical functionalities and business impact.
  • Functional Testing:
  • In this, all the functions in the application are verified with a specific requirement. This is primarily the black-box testing as it is not concerned about the source code of the application. It includes the following steps,

    • Test Input is identified.
    • The test case is executed
    • The actual outcome and the test outcome are compared accordingly.

    Recommended Reading: How To Watch Oprah Meghan Interview

    What Do You Understand By Object Cloning And How Do You Achieve It In Java

    • It is the process of creating an exact copy of any object. In order to support this, a java class has to implement the Cloneable interface of java.lang package and override the clone method provided by the Object class the syntax of which is:
    protected Object clonethrows CloneNotSupportedException
    • In case the Cloneable interface is not implemented and just the method is overridden, it results in CloneNotSupportedException in Java.

    Gear Up For Your Next Technical Interview

    Data Structure and algorithm interview questions in java

    If youâre getting ready for your upcoming Java developer interview, register for our free webinar to get insightful guidance from industry experts on how to nail technical interviews at top companies.

    At Interview Kickstart, weâve trained over 9,000 engineers to land lucrative offers at the biggest tech companies. Our instructors, who are FAANG hiring managers, know what it takes to nail tough tech interviews at top technology companies.

    Also Check: How To Write A Thank You Email Post Interview

    What Is A Friend Function

    A function prototype preceded with the keyword friend and defined outside the class’s scope allows it to access all private and protected members of the class.Even though the prototype for friend function appears in the class definition, friends are not member functions.Coming to the second half of the question, JAVA doesn’t support friend keyword but the concept of friend function can be implemented in JAVA bychoosing appropriate access specifiers for the class and it’s members.

    Top Java Programming Interview Questions

    The Indeed Editorial Team comprises a diverse and talented team of writers, researchers and subject matter experts equipped with Indeed’s data and insights to deliver useful tips to help guide your career journey.

    During a job interview, the hiring manager will not only want to learn more about you personally but about your skills and qualifications. This is especially true when you are applying for a job in programming, as it requires advanced expertise. If you’re applying for a job as a Java programmer, you may be wondering what questions you might face. In this guide, we’ll give you an extensive list of sample questions you might hear during a Java programmer interview, along with some example answers.

    Also Check: How To Make Resume For Job Interview

    More articles

    Popular Articles