0% found this document useful (0 votes)
10 views3 pages

Java

The document contains Java programming questions and answers, including programs to reverse a string, sum digits of an integer, and count characters in a string. It also explains the Object class, types of exceptions in Java, string methods, and the thread life cycle. Additionally, it provides sample code for character counting and discusses thread methods.

Uploaded by

kumar.suman083
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Java

The document contains Java programming questions and answers, including programs to reverse a string, sum digits of an integer, and count characters in a string. It also explains the Object class, types of exceptions in Java, string methods, and the thread life cycle. Additionally, it provides sample code for character counting and discusses thread methods.

Uploaded by

kumar.suman083
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 1: Write a Java Program to reverse a String.

Question 5: Write a Java Program to print the sum of digits of a given integer.
Question 7: Write a Java Program to count the number of characters in a given
String.

package JavaInterviewQuestions;

import java.util.HashMap;

public class CountNumOfCharsInStringExample {

static void characterCount(String inputString) {

// Creating a hashmap object.


HashMap<Character, Integer> hash_map = new HashMap<>();
char[] strArray = inputString.toCharArray();

for (char c : strArray) {


if (hash_map.containsKey(c)) {
hash_map.put(c, hash_map.get(c) + 1);
}

else {
hash_map.put(c, 1);
}

}
// Print the hashmap object which gives the number of each character in String.
System.out.println(hash_map);

public static void main(String[] args) {


// Input value which needs to be passed in the below method.
characterCount("rahulshettyacademy");

***********************************************************************************
***************************************************************
Question 8: Write a Java Program to find the duplicates of a given String.

***********************************************************************************
***************************************************************

2. What is Object Class explain?


Object class is the parent of all classes by default, This is the root class.
Every class has Object as a superclass
Object class is part of java.lang package
You can assign Object of any type to Object class (Ex: Object obj = new Fruit();)
Some useful methods provided by Object Class

toString(): returns the string representation of this object.

getClass(): returns the Class class object of this object


equals(Object obj): compares the given object to this object.

finalize(): this is invoked by the garbage collector before the object is being
garbage collected.

***********************************************************************************
***************************************************************
4. What are the different types of exceptions in java?
Java Exceptions are broadly categorized into Checked and Un Checked exceptions.

Checked exceptions are checked during compile time, Unchecked exceptions are thrown
during run time.

Consider if you are using eclipse IDE, checked exceptions are shown instantly
inside your IDE, however, Unchecked exceptions are thrown only when you execute the
program

***********************************************************************************
***************************************************************

String methods:
substring(): Returns a new string which is the substring of a specified string
toCharArray(): Converts this string to a new character array
indexOf(): Returns the position of the first found occurrence of specified
characters in a string
charAt() : This method returns the character at the specified index (position)
split(): Splits a string into an array of substrings

String string = "Rahul Shetty Academy";

String[] newString = string.split(" ");

// newString will have values like [Rahul, Shetty, Academy]

***********************************************************************************
***************************************************************

12. Explain Thread life cycle briefly? What are some commonly used methods?
Threads are light-weight processes within a process

Life Cycle of Thread

The thread can be in one of the states at a given point in time

NEW: A thread that has not yet started is in this state.

RUNNABLE: A thread executing in the Java virtual machine is in this state.

BLOCKED: A thread that is blocked waiting for a monitor lock is in this state.

WAITING: A thread that is waiting indefinitely for another thread to perform a


particular action is in this state.

TIMED_WAITING: A thread that is waiting for another thread to perform an action for
up to a specified waiting time is in this state.

TERMINATED: A thread that has exited is in this state.

Commonly Used Methods in Java Thread.

start(): Starts the thread.

getState(): It returns the state of the thread.

getName(): It returns the name of the thread.

getPriority(): It returns the priority of the thread.

sleep(): Stops the thread for the specified time.

Join(): Stops the current thread until the called thread gets terminated.

isAlive() : Checks if the thread is alive.

***********************************************************************************
***************************************************************

2. Write a program to reverse a string without using string.reverse() method


6. Write a Java Program to find the largest and smallest word in a string

You might also like