Pages

Showing posts with label Interview Questions. Show all posts
Showing posts with label Interview Questions. Show all posts

Thursday, January 6, 2022

Latest 20+ JMS Interview Questions and Answers

1. Introduction


In this tutorial, We'll learn about JMS interview questions that are frequently asked in 2020. As part of the interview, There are chances to ask some of the questions on JMS area if you have 6 years plus. But, even less experience, it is good to have in the profile on JMS experience. The interviewer will check as messaging is a key aspect of enterprise Java development.
JMS is a popular open-source Messaging API and many vendors such as Apache Active MQ, Websphere MQ, Sonic MQ provides an implementation of Java messaging API or JMS.


Usually, Any interview starts with a basic. If all questions are answered properly then we will go onto the JMS experience project-based questions.

Basics mean What is Topic? What is the Queue? What is Publisher? What is Subscriber? What are a Publisher and Subscriber model? How to configure MQ?
Next level means Questions on a project where you have implemented JMS concepts?

Saturday, December 11, 2021

Top 133 Oracle Exadata Interview Questions

Oracle Exadata Interview Questions:

Study the latest Exadata Interview Questions today here. If you are preparing for Oracle Exadata Interview Questions for Experienced or Freshers, you are at the right place to get the best questions and answers. Gathered all interview questions on Exadata from various websites into one place here. I hope, this makes it easier to prepare for interviews.

Here, We make sure that you refresh your technical knowledge on Exadata(pre-configured combination to run the Oracle Database).

Top 133 Oracle Exadata Interview Questions

Thursday, November 4, 2021

Java - Count Number of Occurrences of Character in a String

1. Overview

In this article, We'll learn how to count the number of occurrences of the given character is present in the string. This problem is solved in the java programming language using traditional loop, replace and java 8 stream api - filter() and reduce() methods.

All solutions are shown with the example code.

Java - Count Number of Occurrences of Character in a String


2. Using for loop & Comparison


Here first, we run the for loop on the input string from index 0 to its length and next compare the each character with the given character. If matches then increase the count variable by one.

By end of the string, count value will be no of occurrences of the given character.

This is the simple and easy to understand.
public class CountNoCharExample1 {

	public static void main(String[] args) {

		String givenString = "hello world";
		
		char givenChar = 'l';

		int strLength = givenString.length();
		int count = 0;

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

			if(givenChar == givenString.charAt(i)) {
				count++;
			}
		}

		System.out.println("no of occurrences of char 'l' is "+count);
	}

}

Output:
no of occurrences of char 'l' is 3

3. Using replace() and length()


Another different approach is to replace given character with the empty string "".  Next, take the difference between the original string length and replaced string length. Here the differences tells the count for the given character.
package com.javaprogramto.programs.strings.count;

public class CountNoCharExample2 {

	public static void main(String[] args) {

		String givenString = "hello world";
		
		char givenChar = 'l';

		int strOriginalLength = givenString.length();
		
		String replacedString = givenString.replace(String.valueOf(givenChar), "");

		int count = strOriginalLength - replacedString.length();
		
		System.out.println("no of occurrences of char 'l' is "+count);
	}

}

Output:
no of occurrences of char 'l' is 3

4. Using Java 8 filter() and count()


Further, we will learn how to get the count of the given character from the string by using java stream api filter() method. This filter() method takes the predicate as condition. If this condition matches then that character is passed to the next stream operation. 

after filter() method, we will call count() method to get the no of instances of character.
package com.javaprogramto.programs.strings.count;

public class CountNoCharStreamExample3 {

	public static void main(String[] args) {

		String givenString = "hello world";

		char givenChar = 'l';

		long count = givenString.chars().filter(c -> c == givenChar).count();

		System.out.println("no of occurrences of char 'l' is " + count);
	}

}
The above code produces the same results as shown in the previous sections.

5. Using Java 8 reduce()


Additionally java 8 is added with the reduce() method which does the reduction operation. 

The below example is to get the count for the given char.
package com.javaprogramto.programs.strings.count;

public class CountNoCharStreamExample4 {

	public static void main(String[] args) {

		String givenString = "hello world";

		char givenChar = 'l';

		long count = givenString.chars()
								.filter(c -> c == givenChar)
								.reduce(0, (a, b) -> a + 1);

		System.out.println("no of occurrences of char 'l' is " + count);
	}

}
Output:
no of occurrences of char 'l' is 3

6. Using third party apis


This problem can be solved by using third party libraries such as apache commons lang, spring framework and gauva api's.

Methods as follows

Apache: StringUtils.countMatches()
Spring Framework: StringUtils.countOccurrencesOf()
Guava: CharMatcher.is().countIn()

7. Conclusion


In this article, we've seen what are the different ways to get the count of the given character in the string using for loop, java 8 streams and third party apis.




Saturday, November 21, 2020

Latest 32 Java 8 Interview Questions + Programming Questions

Java 8 Interview Questions(+ Answers) - Top Java 8 Questions 2020

1. Introduction

In this article, we are going to explore some of the new JDK8 related questions that might pop up during an interview in 2020.

Java 8 interview programs are at the end of the article. First, we have covered the java 8 basic connects. Read till end to find the most useful interview programs.

Java 8 is a platform release packed with new language features and library classes. Most of these new features are geared towards achieving cleaner and more compact code, and some add new functionality that has never been supported in Java.

We have gathered a few but commonly asked everywhere in java 8. Soon publishing some more interview questions only on java 8 stream programs.

Java 8 Most Frequently Asked Interview Questions And Answers in 2020


Saturday, November 16, 2019

Java Program To Find Unmatched values From Two Lists

1. Overview:

In this tutorial, We'll be learning about a java program how to compare two lists and find out the unmatched contents from those two lists. We will demonstrate the example programs in Java and Python languages.

1.1 Example


Input:

List 1: ["File Name 1", "File Name 2", "File Name 3", "File Name 4", "File Name 5", "File Name 6", "File Name 7", "File Name 8"]
List 2: ["File Name 2", "File Name 4", "File Name 6", "File Name 8"]

Output:

Unmatched values: ["File Name 1", "File Name 3", "File Name 5", "File Name 7]

Find Unmatched values From Two Lists


This example is for Strings. If the list is having custom objects such as objects of Student, Trade or CashFlow. But in our tutorial, We will discuss for Employee objects in the list.

We can use any List implementation classes such as ArrayList, LinkedList, Stack, and Vector. For now, all programs in this post are using ArrayList implementation.

2. Java

We'll write a program to remove the duplicates values from list1 based on list2 and operation seems to be more complex but if we use the java collection API methods then our life will become easy because these methods are being tested by many developers every day.

See in our case need to find the unmatched content against list2 from list1.

2.1 String values


List interface has a method named "removeAll" which takes any collection implementation class (in our example it an ArrayList).

removeAll() method removes from the current list all of its elements that are contained in the specified collection that passed to this method as an argument.

Syntax: See the method signature below.

boolean removeAll(Collection c)

Creating two lists which are having files names in it.

// List 1 contains file names from 1 to 8.
List list1 = new ArrayList<>();
list1.add("File Name 1");
list1.add("File Name 2");
list1.add("File Name 3");
list1.add("File Name 4");
list1.add("File Name 5");
list1.add("File Name 6");
list1.add("File Name 7");
list1.add("File Name 8");

//List 2 contains only even number file names.
List list2 = new ArrayList<>();
list2.add("File Name 2");
list2.add("File Name 4");
list2.add("File Name 6");
list2.add("File Name 8");

Now we are going to delete the file names that are already present in list 2 from list 1.

list1.removeAll(list2);

All the elements that are present in list2 are deleted from list1 which means all even number file names are deleted from list1.

Let us take a look at the contents in list1.

[File Name 1, File Name 3, File Name 5, File Name 7]

Note: If list2 is null then will through NullPointerException.

2.2 Custom Objects

What happens if these two lists are having objects instead of String literals? Custom objects such as Employee, Student, Trade or User objects.

We will demonstrate an example with Employee object for now. The same is applicable for any type of object in the list.

Creating an Employee class with id and name.

public class Employee {

 private int id;
 private String name;

 public Employee(int id, String name) {
  this.id = id;
  this.name = name;
 }

 // setter and getter methods.
 
 // Overrding toString method.
 @Override
 public String toString() {
  return "Employee [id=" + id + ", name=" + name + "]";
 }
}

Creating list1 and list2 with employee objects.

List list1 = new ArrayList<>();
list1.add(new Employee(100, "Jhon"));
list1.add(new Employee(200, "Cena"));
list1.add(new Employee(300, "Rock"));
list1.add(new Employee(400, "Undertaker"));

List list2 = new ArrayList<>();
list2.add(new Employee(100, "Jhon"));
list2.add(new Employee(300, "Rock"));

list1 and list2 have common employee objects for id 100 and 200 with the same name. Now we have to remove these two objects from list1. We know that removeAll method removes objects from list1 comparing with list2 objects. We'll call removeAll method.

list1.removeAll(list2);

Now see the objects in list1 after calling removeAll method.

[Employee [id=100, name=Jhon], Employee [id=200, name=Cena], Employee [id=300, name=Rock], Employee [id=400, name=Undertaker]]


Observe that removeAll method is not removed duplicate objects from list1.
To make this code work, we need to override equals() method in Employee class as below.


@Override
public boolean equals(Object obj) {
 Employee other = (Employee) obj;
 if (id != other.id)
  return false;
 if (name == null) {
  if (other.name != null)
   return false;
 } else if (!name.equals(other.name))
  return false;
 return true;
}


Why we need to override equals method is because removeAll method internally compares the contents invoking equals() method on each object. Here the object is an employee so it calls equals method on employee object.

Take a look at the output of list1 now.

[File Name 1, File Name 3, File Name 5, File Name 7]

We have to notice one point here that the same code had worked in the case of String objects because String class already overridden equals method as below.

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String aString = (String)anObject;
        if (coder() == aString.coder()) {
            return isLatin1() ? StringLatin1.equals(value, aString.value)
                              : StringUTF16.equals(value, aString.value);
        }
    }
    return false;
}

All codes are shown are compiled and run successfully in java 12.

3. Python


In Python, finding out the unmatched contents from two lists is very simple in writing a program.

Let us have a look at the following code.

def finder(arr1,arr2):
    eliminated = []

    for x in arr1:
        if x not in arr2:
            eliminated.append(x)
        else:
            pass
    return eliminated

We can sort two lists before for loop as below. I have seen many developers do sorting before comparing the contents. But, actually sorting is not necessary to do.

arr1 = sorted(arr1)
arr2 = sorted(arr2)

4. Conclusion


In this tutorial, We've explored the way to remove the duplicate contents from list 1 against list 2 and finding out the unmatched contents from two lists.

Further discussed comparing two employee lists and finding out unmatched content using removeAll method in Java and Program in Python for the same.

As usual, All examples are shown in this tutorial are available on GitHub.

Java Program to Check Whether a Number is Even Or Odd (3 ways)

1. Introduction


In this tutorial, You will learn how to write a java program to check a given number is even or odd. This can be done by using a mathematic formula or a different approach. Every number in the world must fall under either an even or odd category. Many developers may provide the solution in different ways but finally, they will tell even or not.

java-program-check-even-odd