JAVA INTERVIEW QUESTIONS- TCS, COGNIZANT, WIPRO, ACCENTURE

java image e1729068292284
Spread the love

Java Interview Questions for Freshers (TCS, COGNIZANT, WIPRO, ACCENTURE) 

1. What is keyword in Java?

Answer:

In java, Keywords are the reserved words that have special meaning , These are predefined words by java so they cannot be used as a identifiers like variable names or methods names.

for example , access modifiers(public, private, protected), control flow like if, else, switch, data types this all are the keywords in java.

 

 

2. What is the output of the following program?

class Demo

{

}

Answer:

The above program can compile only but can not run because, the above program has no main() method. The compiler is not depend on main() method but JVM is depend on the main() method for execution.

 

 

3. What are control flow statements? 

Answer:

Control flow statements determine the order in which instructions are executed in a program.  Java provides statements that can be used to control the flow of Java code. Such statements are called control flow statements.

 

 

4. What are the main types of control flow statements in java?

Answer:

Java Provides three types of control statements.

1.Decision making statements

  • if statements
  • if else statements
  • else if ladder
  • nested if statements
  • switch statements

2.Looping statements

    • for loop
    • for each loop
    • while loop
    • do while loop

3. Transfer Statements

    • break
    • continue
    • return

 

 

Java Interview Questions for Freshers (TCS, COGNIZANT, WIPRO, ACCENTURE) 

5. Write Java program for printing even numbers in between 1 to 100 using for loop?

Answer:

public class EvenNumber {

            public static void main(String[] args) {

                     for(int i=0; i<=100; i++){

                                 if(i%2==0)

                                {

                                          System.out.println(i);

                                 }

                     }

             }

}

output:  2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60,62, 64, 66, 68, 70, 72, 74, 76, 78, 80,82, 84, 86, 88, 90, 92, 94, 96, 98, 100.

 

 

6. What will be the output of the following code?

for(int i=1; i<=5; i++)

{

        if(i==3){

        System.out.println(“the value=”+i);

        continue;

}

System.out.println(i);

}

Answer:

1

2

the value=3

4

5

 

7. Write a Java program for printing ASCII value of character.

Answer:

Class Ascii{

          public static void main( String[] args){

          char ch=’a’;

          int value=ch;

          System.out.println(“Ascii value of “+ch+” is”+value);

          }

}

output: Ascii value of a is 97.

 

JAVA INTERVIEW QUESTIONS- DAY 2

8. Write a program to check that the character is vowel or consonent?

Answer:

import java.util.Scanner;

public class VowelOrConsonent {

public static void main(String[] args) {

char ch;

Scanner sc=new Scanner(System.in);

System.out.println(“Enter any character”);

ch=sc.next().charAt(0);

if(ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’ )

System.out.println(ch + ” is vowel”);

else

System.out.println(ch + ” is consonent”);

          }

}

       output: Enter any character

a

a is vowel.

 

 

9. What is the output of the following code? will it compile or execute?

int a=2;

switch(a)

{

}

Answer:

The above code won’t give an error. It will compile and execute also because neither cases nor default is mandatory within switch case.

 

 

10. is break statement within switch case mandatory? what if didn’t use break in switch case?

Answer:

No, Break statements are not mandatory while writing cases in switch. If you don’t use the break statement in a switch case in Java, the program will continue executing the subsequent cases until it encounters a break statement or reaches the end of the switch block.

Without break statement whichever case matches from that case to the end of the statements everything will get executed.

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *