1. What is Typecasting explain with small code snippet?
- Typecasting is converting data from one data type to another data type.
- Typecasting can be applied to both primitive and non-primitive types.
- Typecasting process doesn't alter the data but alters the type.
There are two types:
- Implicit casting
- Explicit casting
Implicit casting: This is known as automatic typecasting. Usually, this happens when you converting a lower data type to a higher data type
Example Code Snippet
class Main {
public static void main(String args[]) {
int i = 10;
float f = i;
System.out.println("Value of f = " +f);
}
}
Output:
Value of f = 10.0
Explicit TypeCasting
Explicit casting involves assigning a data type of high range to a lower range. This casting needs to be done manually
Example Code Snippet
class Main {
public static void main(String args[]) {
float f = 3.142f;
int i = (int) f;
System.out.println("Value of i = " +i);
}
}
Output:
Value of i = 3
Note: Why do we need typecasting?
Typecasting is needed to get access to fields and methods declared on the target type or class. You cannot access them with any other type.
2. Write a program to reverse a string without using string.reverse() method
Example Code Snippet
class Main {
public static void main(String args[]) {
String string = "Rahul Shetty Academy";
String reverse = "";
for (int i = string.length() - 1; i >= 0; --i) {
reverse += string.charAt(i);
}
System.out.println(reverse);
}
}
Output: ymedacA yttehS luhaR
import java.util.*;
class Main {
static void countChars(String string) {
HashMap<Character, Integer> charCount = new HashMap<Character, Integer>();
char[] charArray = string.toCharArray();
for (char c : charArray) {
if (charCount.containsKey(c)) {
charCount.put(c, charCount.get(c) + 1);
} else {
charCount.put(c, 1);
}
}
for (Map.Entry entry : charCount.entrySet()) {
System.out.println("Character "+entry.getKey() + " Occurred " + entry.getValue() +" Time(s)");
}
}
public static void main(String[] args) {
countChars("java");
}
}
Output:
Character a Occurred 2 Time(s)
Character v Occurred 1 Time(s)
Character j Occurred 1 Time(s)
4. Write a program to check the magic number
The number is called a Magic number, If the sum of its digits is calculated till a single digit recursively by adding the sum of the digits after every addition. If the single-digit comes out to be 1, then the number is a magic number.
Example 1:
Number: 1081
1081 = 1+0+8+1 Sum is 10
10 = 1+0 Sum 1
So 1081 is Magic number
Example 2:
Number: 1056
1056 = 1+0+5+6 Sum is 12
12 = 1+2 Sum is 3
So 1056 is not a Magic number
Solution:
class Main {
public static void main(String[] args) {
int sum = 0;
int number = 1081;
int n = number;
while (n > 0 || sum > 9) {
if (n == 0) {
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
if (sum == 1) {
System.out.println(number + " is a Magic Number");
} else {
System.out.println(number + "is not a Magic Number");
}
}
}
Output:
1081 is a Magic Number
5. Write a program to check if two strings are an anagram
An anagram is if two strings are having exactly the same chars and however the order of chars are different.
Example 1:
String 1 = ABCD
String 2: BDCA
The String 1 and String 2 are anagrams
Example 2:
String 1: Race
String 2: Care
String 1 and String 2 are anagrams
Solution:
import java.util.*;
public class Main {
public static void main(String[] args) {
String string1 = "race";
String string2 = "care";
boolean isAnagram = false;
if (string1.length() != string2.length()) {
isAnagram = false;
}
else{
char[] char1 = string1.toCharArray();
char[] char2 = string2.toCharArray();
Arrays.sort(char1);
Arrays.sort(char2);
for (int i = 0; i < char1.length; i++) {
if (char1[i] != char2[i]) {
isAnagram = false;
break;
}
isAnagram = true;
}
}
if (isAnagram)
System.out.println(string1 + " and "+string2+ " are Anagram");
else
System.out.println(string1 + " and "+string2+ " are Not Anagram");
}
}
Output:
race and care are Anagram
6. Write a program to create a deadlock in java
Deadlock is the situation when a first thread is waiting for an object, that is acquired by a second thread and the second thread is waiting for an object lock that is acquired by the first thread. Since both threads are waiting for each other to release the lock. This condition is called deadlock.
Solution:
class Main {
public static void main(String[] args) {
Object resource1 = new Object();
Object resource2 = new Object();
Thread t1 = new Thread() {
public void run() {
synchronized (resource1) {
System.out.println("Thread 1: locked resource 1");
try {
Thread.sleep(100);
} catch (Exception e) {
}
synchronized (resource2) {
System.out.println("Thread 1: locked resource 2");
}
}
}
};
Thread t2 = new Thread() {
public void run() {
synchronized (resource2) {
System.out.println("Thread 2: locked resource 2");
try {
Thread.sleep(100);
} catch (Exception e) {
}
synchronized (resource1) {
System.out.println("Thread 2: locked resource 1");
}
}
}
};
t1.start();
t2.start();
}
}
Output:
Thread 1: locked resource 1
Thread 2: locked resource 2
7. Consider I have data like below distance to the city from your hometown
Bangalore, 560
Chennai, 890
Hyderabad, 566
Mumbai, 788
New Delhi, 1000
Write a program that stores the above data in Map, and print them back by iterating over each element.
Solution:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
class Main {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("Bangalore", 560);
map.put("Chennai", 890);
map.put("Hyderabad", 566);
map.put("Mumbai", 788);
map.put("New Delhi", 1000);
for(Entry<String,Integer> entry:map.entrySet()) {
System.out.println("Distance from Hometown to " + entry.getKey()+" is " + entry.getValue()+" Kms");
}
}
}
Output:
Distance from Hometown to New Delhi is 1000 Kms
Distance from Hometown to Chennai is 890 Kms
Distance from Hometown to Mumbai is 788 Kms
Distance from Hometown to Hyderabad is 566 Kms
Distance from Hometown to Bangalore is 560 Kms
Course Alert
If you are interested in landing a SDET or Sr. QA Automation Engineer role, check out our popular SDET course to learn the fundamental skills required an SDET needs to perform at work. Also, I have another popular course which covers Top 150+ Interview questions most commonly asked in SDET / Automation interviews.
A Quick Note
Please remember all these courses comes with 100% Lifetime access and free updates for Lifetime!!
Please click on the link Sr.QA and SDET Interview Questions in Java – Part 5 to continue to part-5 series of this article.