What is Java?
Java is simple language
easy to understand & based on c++
Java removed many confusing & easrly used features.
like:explicit pointers, operater overloading
by default java provides Garbage collection automatically & memory management
Java is platform independent
Object oriented
Java is robust langulage: beacause it is used in consumer applicatoins, mission critical applications & navigation systems.
What is java Main method
public static void main (String[] args)
public-> so JVM can execute the method from anywhere
static-> method is to be called without object
void-> main method doesnt returns anything.
main()-> configured in JVM
String[]-> main method accepts a single arguments of an array of elements of type string.
System.out.println()
System->pre defined class
out->variable type output stream connected to console
println()-> method
is Main method is compulsory in Java?
<5 main method is not compulsory in java because static block was executed & doesnt need object to call method.
>=6 main method is compulsory .
compilation is successful but we will get run time error as
"Erro main method not found in class test. Please define the main method as public static vooid main(String[] args)"
normal Interview Questions and answers
>write a program to check wheather given string is a plindrome or not?
ex: maaM -->true
code:
public class program{
public static void main(String... args)
{
System.out.println(isPalindrome(""Maam)) ;
}
public boolean isPalindrome(String word)
{
word = word.toLowerCase();
boolean result = false;
String myword = "";
for(int i = word.lenght()-1;i>=0;i--)
{
myword = myword + word.charAt(i);
}
if(word.equals(myword)
{ result = true;}
return result;
}
}
Question:
Java is simple language
easy to understand & based on c++
Java removed many confusing & easrly used features.
like:explicit pointers, operater overloading
by default java provides Garbage collection automatically & memory management
Java is platform independent
Object oriented
Java is robust langulage: beacause it is used in consumer applicatoins, mission critical applications & navigation systems.
What is java Main method
public static void main (String[] args)
public-> so JVM can execute the method from anywhere
static-> method is to be called without object
void-> main method doesnt returns anything.
main()-> configured in JVM
String[]-> main method accepts a single arguments of an array of elements of type string.
System.out.println()
System->pre defined class
out->variable type output stream connected to console
println()-> method
is Main method is compulsory in Java?
<5 main method is not compulsory in java because static block was executed & doesnt need object to call method.
>=6 main method is compulsory .
compilation is successful but we will get run time error as
"Erro main method not found in class test. Please define the main method as public static vooid main(String[] args)"
normal Interview Questions and answers
>write a program to check wheather given string is a plindrome or not?
ex: maaM -->true
code:
public class program{
public static void main(String... args)
{
System.out.println(isPalindrome(""Maam)) ;
}
public boolean isPalindrome(String word)
{
word = word.toLowerCase();
boolean result = false;
String myword = "";
for(int i = word.lenght()-1;i>=0;i--)
{
myword = myword + word.charAt(i);
}
if(word.equals(myword)
{ result = true;}
return result;
}
}
Question:
User interface contains two types of user input controls: TextInput, which accepts all characters and NumericInput, which accepts only digits.
Implement the class TextInput that contains:
Implement the class TextInput that contains:
- Public method void add(char c) - adds the given character to the current value
- Public method String getValue() - returns the current value
- Inherits TextInput
- Overrides the add method so that each non-numeric character is ignored
TextInput input = new NumericInput(); input.add('1'); input.add('a'); input.add('0'); System.out.println(input.getValue());
Answer:
class TextInput {
String input = "";
String output = "";
public void add(char ch)
{
this.input = this.input+ch;
}
public String getValue(){
return this.input;
}
}
class NumericInput extends TextInput {
public void add(char ch)
{
if(Character.isDigit(ch))
{
super.input = super.input + ch;
}
}
}
public class UserInput {
public static void main(String[] args) {
TextInput input = new NumericInput();
input.add('1');
input.add('a');
input.add('0');
input.add('0');
input.add('h');
System.out.println(input.getValue());
}
}
String input = "";
String output = "";
public void add(char ch)
{
this.input = this.input+ch;
}
public String getValue(){
return this.input;
}
}
class NumericInput extends TextInput {
public void add(char ch)
{
if(Character.isDigit(ch))
{
super.input = super.input + ch;
}
}
}
public class UserInput {
public static void main(String[] args) {
TextInput input = new NumericInput();
input.add('1');
input.add('a');
input.add('0');
input.add('0');
input.add('h');
System.out.println(input.getValue());
}
}
Write a function that, given a list and a target sum,
returns zero-based indices of any two distinct elements whose sum is
equal to the target sum. If there are no such elements, the function
should return null.
For example, findTwoSum(new int[] { 1, 3, 5, 7, 9 }, 12) should return any of the following tuples of indices:
For example, findTwoSum(new int[] { 1, 3, 5, 7, 9 }, 12) should return any of the following tuples of indices:
- 1, 4 (3 + 9 = 12)
- 2, 3 (5 + 7 = 12)
- 3, 2 (7 + 5 = 12)
- 4, 1 (9 + 3 = 12)
answer
public class TwoSum {
public static int[] findTwoSum(int[] list, int sum) {
for(int i=0;i<list.length-1;i++)
{
for(int j=i+1;j<list.length;j++)
{
//System.out.println(i + " " +j);
if(list[i]+list[j] == sum)
{
return new int[] {i,j};
}
}
//System.out.println(list[i]);
}
return new int[]{0,0};
}
public static void main(String[] args) {
int[] indices = findTwoSum(new int[] { 1, 3, 5, 7, 9 }, 12);
System.out.println(indices[0] + " " + indices[1]);
}
}
public static int[] findTwoSum(int[] list, int sum) {
for(int i=0;i<list.length-1;i++)
{
for(int j=i+1;j<list.length;j++)
{
//System.out.println(i + " " +j);
if(list[i]+list[j] == sum)
{
return new int[] {i,j};
}
}
//System.out.println(list[i]);
}
return new int[]{0,0};
}
public static void main(String[] args) {
int[] indices = findTwoSum(new int[] { 1, 3, 5, 7, 9 }, 12);
System.out.println(indices[0] + " " + indices[1]);
}
}