Tag Archives: Java Interview Questions

Java Interview Questions – 2

Ques 1. What are the Object and Class classes used for?

Ans. The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program.

 

Ques 2. Can you write Java code for declaration of multiple inheritance in Java ?

Ans.

Class C extends A implements B

{

}

 

Ques 3. What do you mean by multiple inheritance in C++ ?

Ans. Multiple inheritance is a feature in C++ by which one class can be of different types. Say class teachingAssistant is inherited from two classes say teacher and Student.

Continue reading Java Interview Questions – 2

Java Interview Questions – 1

Ques 1. How can you achieve Multiple Inheritance in Java?

Ans. Java’s interface mechanism can be used to implement multiple inheritance, with one important difference from c++ way of doing MI: the inherited interfaces must be abstract. This obviates the need to choose between different implementations, as with interfaces there are no implementations.

 

Ques 2. Replacing Characters in a String

Ans.

// Replace all occurrences of ‘a’ with ‘o’

String newString = string.replace(‘a’, ‘o’);

Replacing Substrings in a String

static String replace(String str,

String pattern, String replace) {

int s = 0;

int e = 0;

StringBuffer result = new StringBuffer();

while ((e = str.indexOf(pattern, s)) >= 0) {

result.append(str.substring(s, e));

result.append(replace);

s = e+pattern.length();

}

result.append(str.substring(s));

return result.toString();

}

Converting a String to Upper or Lower Case

// Convert to upper case

String upper = string.toUpperCase();

// Convert to lower case

String lower = string.toLowerCase();

Converting a String to a Number

int i = Integer.parseInt(“123”);

long l = Long.parseLong(“123”);

float f = Float.parseFloat(“123.4”);

double d = Double.parseDouble(“123.4e10”);

Breaking a String into Words

String aString = “word1 word2 word3”;

StringTokenizer parser =

new StringTokenizer(aString);

while (parser.hasMoreTokens()) {

processWord(parser.nextToken());

Continue reading Java Interview Questions – 1