Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Showing posts with label JAVA SE PROGRAMMING INTERVIEW QUESTIONS AND ANSWERS. Show all posts
Showing posts with label JAVA SE PROGRAMMING INTERVIEW QUESTIONS AND ANSWERS. Show all posts

Saturday, 24 July 2021

Write a Java Program To Count Letters in String

How To Count Letters in String in Java


In the last post, we have discussed how to find character or character position in string in java with simple examples and now here we are going to create a simple example for how to count letters in java string with examples.

So let's start with a useful example for finding the number of letters in a given java String.

The below example gives you a total number of letters count in a given java string i.e (given string is "sun45tt" and letters in this give string is suntt i.e 5).

Java Program to Count Letters in String

class LettersCountInString
{
public static void main(String args[])
{
String str = "sun45tt";
int count = 0; 
for(int i = 0; i<str.length(); i++)
{
if(Character.isLetter(str.charAt(i)))
{
count++;
}
}
System.out.println(count);
}
}

Output: 5

Here we discussed, how to write a java program to count letters in string with a simple example.


Share:

Friday, 23 July 2021

How Do I Find a Character in a String in Java

 Searching Characters in a String in Java

In the last article, we have learned how do I find a first non-repeated character in string in java and now here we are going to make a java program for how do I find a character in a string with a simple example.

Here we will find the index position of a character given in a java string by using the pre-defined method of a java String Object.

Here we will find the character position in a sting by using the indexOf( ) method of the String class.

So let's start searching for specific character in string.

Find Character in Java String

class FindingCharacterInString
{
public static void main(String args[])
{
String str = "how are you men";
int indexPosition = str.indexOf('a'); //finding character a String
System.out.println("Index Position of character a is  "+indexPosition);
}
}

Output : Index Position of character is 4

Note the above program for finding a character will give you the first occurrence of the character's position not the second occurrence of a character.

Let's understand by simple example where we again find the character in a string but with a different character.

class FindingCharacterInString
{
public static void main(String args[])
{
String str = "how are you men";
int indexPosition = str.indexOf('o'); //finding character String
System.out.println("Index Position of character is  "+indexPosition);
}
}

Output: Index Position of character is 1

So here we discussed some useful examples for how do i find a character in a String in java.
Share:

Tuesday, 24 October 2017

Interface Programming Interview Questions in Java

Java Interface Programming Interview Questions

Interface Programming in Java

Now, Here we are going to discuss some important interface programming interview questions in java. Java interface is also a most important topic and mostly asked in any core java interviews.

Let's see some questions related to interface in java programming which is frequently asked in java interviews.


(1) What is the output of following program?

interface My
{
void fun();
}
class My1 implements My
{
void fun()
{
System.out.println("hi");
}
public static void main(String args[])
{
My1 m = new My1();
m.fun();
}
}

Output: compile time error

Explanation: By default all the methods of an interface are public and abstract and when we will implement or override a method in a class which implements interface then we have to use public modifier with method e.g public void fun(){} in My1 class.


(2) What is the output of the following program?

interface Demo
{
void show()
{
System.out.println("hi, i am good");
}
void run();
}
class Test implements Demo
{
public void run()
{
System.out.println("run fast");
}
public static void main(String args[])
{
Test t = new Test();
t.run();
}
}

Output: compile time error

Explanation: We can't keep method with body in interface before java 8 version but it possible to keep method body in interface of java 8 by using 'default' keyword.


(3) Can you identify the error in below program?

interface Demo
{
private int b;
}

Output: Illegal modifier for field b

Explanation: By default all the fields of an interface is public, static and final. 


(4) What is the output of the following program?

interface First
{
void test();
}
class Second implements First
{
public void test()
{
System.out.println("pass");
}
public static void main(String args[])
{
First f = new First();
f.test();
}
}

Output: compile time error

Explanation: We can't instantiate interface in java.


(5) What is the output of following program?

interface First
{
First()
{
System.out.println("hello");
}
void show();
}
class Test implements First
{
public void show()
{
System.out.println("how are you");
}
public static void main(String args[])
{
Test t = new Test();
t.show();
}
}


Output: compile time error

Explanation: We can't keep constructor in an interface.


(6) What is the output of a below program?

interface My
{
int i = 10;
}
class My1 implements My
{
void change()
{
i = 40;
System.out.println(i);
}
public static void main(String args[])
{
My1 m = new My1();
m.change();
}
}

Output: compile time error


(7) What is the output of following program?

interface First
{
void show();
}
interface Second
{
void display();
}
class Test implements First,Second
{
public void show()
{
System.out.println("first interface");
}
public void display()
{
System.out.println("second interface");
}
public static void main(String args[])
{
Test t = new Test();
t.show();
t.display();
}
}

Output: first interface
             second interface


(8) Declaration of below code is correct or incorrect?

class A
{
//statements
}
interface B extends A
{}

Output: incorrect because interface cannot extend a class

(9) Declaration of below code is correct or incorrect?

interface A
{
//statements
}
interface B extends A
{}

Output: correct because an interface can extend one or multiple interface at a time.


(10) What is the output of a following program?


interface First

{

void show();

}
interface Second
{
void display();
}
class Test implements First,Second
{
public void show()
{
System.out.println("first interface");
}
public void display()
{
System.out.println("second interface");
}
public static void main(String args[])
{
First f = new Test();
f.show();
f.display();
}
}

Output: compile time error

Explanation: To run the above program you can create....

1) Test t = new Test();
     t.show();
     t.show();

2) First f = new Test();
    Second s = new Test();
    f.show();
    s.display();


You can check String programming interview questions and inheritance programming interview questions in java.

This is the basic and important java interface programming interview questions and answers.  
Share:

Monday, 18 September 2017

Java Array Programming Interview Questions and Answers

Array Programming Interview Questions in Java

Array Coding Interview Questions in Java

This is the most important article for core java interview. Here we are going to discuss some java array programming interview questions and answers one-by-one in detail.

In the last post, you have learned String programming interview questions but here we will see array coding interview questions in java.

Let's start programming questions on array.


(1) What is the output of following program?

class Demo1
{
public static void main(String args[])
{
int i[] = new int[0];
System.out.println(i[0]);
}
}

Output: java.lang.ArrayIndexOutOfBoundsException...run-time error


(2) Can we pass the negative number in array size declaration?

class Demo2
{
public static void main(String args[])
{
int i[] = new int[-3];
------
------
}
}

Output: java.lang.NegativeArraySizeException...run-time exception


(3) Write a java program to find intersection between two arrays?

In this example,we will find the common elements between 2 arrays.

class Demo3
{
public static void main(String args[])
{
String s1[] = {"red", "pink", "orange", "black"};
String s2[] = {"pink", "brown", "red", "white"};
//using HashSet class
HashSet<String> hs = new HashSet<String>();
for(int i = 0; i<s1.length; i++)
{
for(int j = 0; j<s2.length; j++)
{
if(s1[i].equals(s2[j]))
{
hs.add(s1[i]);
}
}
}
System.out.println(hs);
}
}

Output: [red, pink]


(4) Write a java program to sort an array elements?

By the help of Arrays.sort() method we can easily sort the array elements in ascending order and this sort() method internally uses quick sort algorithm to sort the elements of an array.

class Demo4
{
public static void main(String args[])
{
int[] i = {4, 1, 99, 3, 7};
Arrays.sort(i);
System.out.println(Arrays.toString(i));
}
}

Output: [1, 3, 4, 7, 99]


(5) Write a java program to reverse an array?

This is the simple java programs on array where we reverse the elements of an array in java.

class Demo5
{
public static void main(String args[])
{
int n[] = {1,2,3,4,5,6,7,8,9,10};
System.out.println("Array elements before reverse");

for(int i = 0; i<n.length; i++)
{
System.out.print(n[i]+" ");
}
for(int i = 0; i<n.length/2; i++)
{
int temp = n[i];
n[i] = n[n.length-1-i];
n[n.length-1-i] = temp;
}
System.out.println("\n Array elements after reverse");
for(int i = 0; i<n.length; i++)
{
System.out.print(n[i]+" ");
}
}
}

Output: Array elements before reverse
             1 2 3 4 5 6 7 8 9 10
             Array elements after reverse
             10 9 8 7 6 5 4 3 2 1


(6) Write a java program to find duplicate elements in array?

class Demo6
{
public static void main(String args[])
{
String[] s = {"apple", "orange", "apple", "banana"};
HashSet<String> hs = new HashSet<String>();
for(String duplicate : s)
{
if(!hs.add(duplicate))
{
System.out.println(duplicate);
}
}
}
}

Output: apple


(7) Write a java program to find minimum and maximum value in an array?

class Demo7
{
public static void main(String args[])
{
int []a = {1,5,8,9,50,100,200};
//assign first element of an array to largest and smallest
int smallest = a[0];
int largest = a[0];
for(int i = 1; i<a.length; i++)
{
if(a[i]>largest)
largest = a[i];
else if(a[i]<smallest)
smallest = a[i];
}
System.out.println("Largest number is "+ largest);
System.out.println("Smallest number is "+ smallest);
}
}

Output: Largest number is 200
             Smallest number is 1


(8) How to compare two arrays in java?

If two array are of the same size and data types then we can use Arrays.equals() method for comparison.

import java.util.*;
class Demo8
{
public static void main(String args[])
{
int a[] = {1, 2, 3, 4};
int b[] = {9, 8, 7, 6};
int c[] = {1, 2, 3, 4};
System.out.println(Arrays.equals(a, b));
System.out.println(Arrays.equals(b, c));
}
}

Output: false
              true

Read More:

Top 10 Programming Interview Questions.
String Programming Interview Questions.
Interface Programming Interview Questions.
Inheritance Programming Interview Questions.
Difference Between Array and Collection in Java.
Some Basic Programs in Java.
Number Pattern Programs in Java.

Above all the programming questions on array in java are quite useful for any core java programming interview questions.

Share:

Wednesday, 13 September 2017

String Programming Interview Questions in Java

String Programming Interview Questions

Java String Programming Interview Questions and Answers

Here we are gonna to discuss some string programming interview questions in java. String handling is the most important topic in core java.

In this article you will see most frequently asked java string programming interview questions and answers.

Let's start java string programs asked in interview one-by-one.

(1) What is the output of following program?

class Test1
{
public static void main(String args[])
{
String a = "javatutorial95"+100+200;
System.out.println(a);
}
}

Output: javatutorial95100200


(2) What is the output of following program?

class Test2
{
public static void main(String args[])
{
String a = 10+20+"javatutorial95"+20+30;
System.out.println(a);
}
}

Output: 30javatutorial952030


(3) Write a program to reverse a String in java without using String API?

class Test3
{
public static void main(String args[])
{
String s = "Best Country";
String reverse = "";
for(int i = s.length()-1; i>=0; --i)
{
reverse +=s.charAt(i);
}
System.out.println(reverse);
}
}

Output: yrtnuoC tseB


(4) Write a java program to check String is palindrome or not?

class Test4
{
public static void main(String args[])
{
String name = "MADAM";//String to be checked for palindrom
String reverse = "";
for(int i = name.length()-1; i>=0; --i)
{
reverse += name.charAt(i);
}
System.out.println(reverse);

if(reverse.equalsIgnoreCase(name))
{
System.out.println("String is palindrome");
}
else
{
System.out.println("String is not palindrome");
}
}
}

Output: MADAM
             String is plindrome


(5) Write a java program to find the duplicate words and their number of occurrences in a string?

import java.util.*;
class Test5
{
static void duplicateWords(String string1)
{
//splitting string1 into words
String[] words = string1.split(" ");

//creating HashMap with word as key and count as value
HashMap<String, Integer> wordcount = new HashMap<String, Integer>();

//checking each words
for(String word : words)
{
if(wordcount.containsKey(word.toLowerCase()))
{
wordcount.put(word.toLowerCase(), wordcount.get(word.toLowerCase())+1);
}
else
{
wordcount.put(word.toLowerCase(), 1);
}
}
Set<String> wordsstring = wordcount.keySet();
for(String word : wordsstring)
{
if(wordcount.get(word)>1)
{
System.out.println(word+"  :  "+wordcount.get(word));
}
}
}

public static void main(String args[])
{
duplicateWords(dog is Dog not cat);
duplicateWords(cat is Dog not cat and cat);
}
}

Output: dog : 2
              cat : 3


(6) Write a java program to count the number of words in a string?

import java.util.*;
class Test6
{
public static void main(String args[])
{
System.out.println("Enter your string");
//using Scanner class
Scanner s = new Scanner(System.in);
String s1 = s.nextLine();
String[] words = s1.trim().split(" ");
System.out.println("Number of word in a string "+words.length);
}
}


(7) Write a java program to count the number of occurrences of each characters in string?

Occurrence of characters in string e.g "javatutorial" j = 1, a = 3, v = 1..

import java.util.*;
class Test7
{
static void characterCount(String string1)
{
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
//converting given string to char array
char[] ch = string1.toCharArray();
//checking eahc char of ch
for(char c : ch)
{
if(hm.containsKey(c))
{
hm.put(c, hm.get(c)+1);
}
else
{
hm.put(c, 1);
}
}
System.out.println(hm);
}
public static void main(String args[])
{
characterCount("India is a good country");
}
}


(8) How to check total number of vowels in a java string?

There are 5 vowels in a to z alphabet e.g 'a', 'e', 'i', 'o', 'u'.

import java.util.*;
class Test8
{
public static void main(String args[])
{
System.out.println("Enter some string");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char chars[] = str.toCharArray();
int count = 0;
for(char c : chars)
{
switch(c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
break;
}
}
System.out.println("Number of vowels in string "+count);
}
}

Output: Enter some string
              i love you
              Number of vowels in string 5


(9) How to sort the string without using string API in java?

class Test9
{
public static void main(String args[])
{
String original = "zfabcd";
int j = 0;
char temp = 0;
char chars[] = original.toCharArray();
for(int i = 0; i<chars.length; i++)
{
for(j = 0; j<chars.length; j++)
{
if (chars[j]>chars[i])
{
temp = chars[i];
chars[i]=chars[j];
chars[j]=temp;
}
}
}
for(int k = 0; k<chars.length; k++)
{
System.out.println(chars[k]);
}
}
}

Output: a
             b
             c
             d
             f
             z

Read More:

Java Array Programming Interview Questions.
Top 10 Java Coding Interview Questions.
Difference Between String and StringBuffer in Java.

Above all the java string programs examples are quite useful in any core java interview. 

Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Link

Translate