Programming





Interview Question and Answers Part 3

Interview Question and Answers Part 3

by | 31-Mar-2020
Java

Tags:

Share: 


  1. What is the difference between & and && or | and ||?

In the Bitwise operator, if the first operator evaluates to true in OR (|) and False in AND (&) then the second expression will also be evaluated. But, in Logical Operator, if the first operand evaluates to true in OR (|) and False in AND (&) then the second expression will not be evaluated.

  1. What is the difference between the >> and >>> operators?

The >> operator is also called Arithmetic Shift Right which carries a signed bit when shifting right. The >>> operator is also called a logical shift right which does not carry any signed bit.

  1. Can shift operators be applied to float types?

No, shift operators can be applied only to integer or long types

  1. What happens to the bits that fall off after shifting?

They are discarded.

  1. What values of the bits are shifted in after the shift?

In case of signed left shift >> the new bits are set to zero. But in case of signed right shift << it takes the value of most significant bit before the shift that is if the most significant bit before shift is 0 it will introduce 0, else if it is 1, it will introduce 1.

  1. What are operators and what are the various types of operators available in Java?

Operators are special symbols used in expressions. The following are the types of operators:

  • Arithmetic operators
  • Assignment operators
  • Increment & Decrement operators
  • Logical operators
  • Bitwise operators
  • Comparison/Relational operators
  • Conditional operators

 

  1. Can you write an if statement without else statement?

Yes

  1. Can you write else statement without if statement?

No

  1. What restrictions are placed on the values of each case of a switch statement?

During compilation, the values of each case of a switch statement must evaluate to a value that is an integer or compatible with an integer value.

  1. Can a for statement loop indefinitely?

Yes, for statement can loop indefinitely. Here’s the code: for(;;) ;

  1. What is the difference between a break statement and a continue statement?

A break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.

  1. What is the difference between a do and a while loop?

A while loop checks at the beginning of a loop to see whether the next loop iteration should occur. A do-while statement checks at the end of a loop to see whether the next iteration of a loop should occur. A do loop will always execute at least once.

  1. What is meant by variable?

Variables are locations in memory that can hold values. Before assigning any value to a variable, it must be declared.

  1. What are the kinds of variables in Java? What are their uses?

Java has three kinds of variables namely:

Instance Variable: Local variables are used inside blocks as counters or in methods as temporary variables and are used to store information needed by a single method.

Local Variable: Instance variables are used to define attributes or the state of a particular object and are used to store information needed by multiple methods in the objects.

Class Variable: Class variables are global to a class and to all the instances of the class and are useful for communicating between different objects of all the same class or keeping track of global states.

  1. What is literal? How many types of literals are there?

A literal represents a value of a certain type. There are different types of literals namely number literals, character literals, boolean literals, string literals, etc.

  1. What is an array?

An array is an object that stores a list of homogeneous datatype elements.

  1. How do you declare an array?

Array variable indicates the type of object that the array holds. Ex:

int intArr[];

String stringArr[];

  1. Which of the following declares an array of string objects?
  2. String[ ] s;
  3. String [ ]s:
  4. String[ s];
  5. String s[ ];

a, b and d are the correct way of declaring an array.

  1. What is a static variable?

Static variables are shared by all instances of a class.

  1. What is final variable?

Final variable’s value cannot be changed. It becomes a constant.

";"";"Here is a list of one liner Question and Answers which will be helpful in interviews.";"Administrator";"Java";"2020-03-31 00:00:00+00";"''" 31;"Interview Question and Answers Part 2";"
  1. What is the argument type of a main( ) method?

The argument type is a String array

  1. Can a program have multiple main methods in the same class?

No, the program will not compile if we do so.

  1. Can a main() method be overloaded?

Yes, by method overloading it can be done.

  1. Why do we need public static void main(String args[]) method in Java?

We need them because:

public: The method can be accessed outside the class/package.

static: this method is static so that we don’t need to create an instance.

void: Your application need not return a value, as the JVM launcher would return the value when it exits.

main(): This is the entry point for the application. The JVM is written to name this method as main().

  1. What is the use of String args[] in the main() method?

String args[] will help the program to receive command-line arguments. JVM will create an array of String and will store the received arguments inside String args[].

  1. Can main method be declared final?

Yes, the main method can be declared final, in addition to being public static.

  1. How can one prove that the array is not null but empty using one line of code?

Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.

  1. What is the first argument of the String array in main() method?

The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.

  1. If I do not provide any arguments on the command line, then the String array of main() method will be empty or null?

It is empty. But not null.

  1. How can one prove that the array is not null but empty using one line of code?

Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.

  1. Can an application have multiple classes having the main method?

Yes, an application can have multiple classes having the main method, but while starting the application, mention the class name which is to be run i.e the class name which is to be executed. The JVM will only look for the main() method in that class which you have mentioned. Hence there will not be any conflict amongst the multiple classes having the main method.

  1. What is the use of -Xms and -Xmx?

It is used to create or increase the size of the JVM memory. java -Xms64m -Xmx256m HelloWorld. Requesting for min 64MB and max 256MB.

  1. What is the output of the following line of code? System.out.println("// Is this a comment?"); Output

// Is this a comment?

  1. How to assign null value to int?

int a=10;

Integer a1=(Integer)a;

a1=null;

a=a1;

System.out.println(a);

  1. Why char uses 2 byte in java and what is \u0000 ?

Because java uses the Unicode system rather than the ASCII code system. \u0000 is the lowest range of Unicode system.

  1. What is the difference between declaring a variable and defining a variable?

In declaration, we just mention the type of the variable and its name. We do not initialize it.

But defining means declaration + initialization.

For e.g. String s; is just a declaration while String s = new String ("abcd"); Or String s = "abcd"; are both definitions.

  1. What is the difference between an argument and a parameter?

While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.

  1. What happens if you don’t initialize an instance variable of any of the primitive types in Java? Java by default initializes it to the default value for that primitive type. Thus an int will be initialized to 0, a boolean will be initialized to false, etc.
  2. When does a static variable memory will destroy?

At the time of JVM shut down.

  1. Which arithmetic operations can result in the throwing of an ArithmeticException?

/ (Division) and % (Modulus) can result in the throwing of an ArithmeticException.


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.