Core java - Interview Questions and Answers for 'Computer solutions' - 3 question(s) found - Order By Newest Frequently asked to fresh graduates and less experienced. Q1. Why do we write public static void main ? Can we use some other syntax too for main ? Core Java Admin info@buggybread.com
Ans. 1. public is the access modifier that makes the method accessible from anywhere, static is the keyword that makes it accessible even without creating any object, void means it doesn't return anything , String args[] is the array of argument that the method receives.
2. If we use main without the string args , it will compile correctly as Java will treat it as just another method. It wont be the method "main" which Java looks for when it looks to execute the class and hence will throw
Error: Main method not found in class , please define the main method as:
public static void main(String[] args)
3. Main is not a keyword but a special string that Java looks for while initiating the main thread. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   main method Asked in 4 Companies basic   frequent Try 1 Question(s) TestRelated Questions Which of the following is false about main method ?
a. It should be declared public and static
b. it should have only 1 argument of type String array
c. We can override main method
d. We can overload main method Can we have multiple main methods in a single class ? Can we overload main method in Java ? What will happen if static modifier is removed from the signature of the main method? How can we execute a Java class independently if it doesn't have a static main method ? Can we declare a main method as abstract ? Do all java classes need a main method? Can we declare the main method as private ? Why main method is declared static ? Q2. Why java doesn't support multiple Inheritance ?
or
Explain Java Diamond Problem. Core Java 2017-01-09 11:45:34
Ans. class A {
void test() {
System.out.println("test() method");
}
}
class B {
void test() {
System.out.println("test() method");
}
}
Suppose if Java allows multiple inheritance like this,
class C extends A, B {
}
A and B test() methods are inheriting to C class.
So which test() method C class will take? As A & B class test() methods are different , So here we would Facing Ambiguity. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  multiple inheritance  object oriented programming (oops)  oops concepts  diamond problem Asked in 20 Companies basic   frequent Q3. Write a Program to check if 2 strings are Anagrams ? Core Java 2016-11-30 20:52:37
Ans. public void checkIfAnagram(String str1,String str2){
boolean anagram = true;
for(char c:str1.toCharArray()){
if(!str2.contains(String.valueOf(c))){
System.out.println("Strings are Anagrams");
anagram = false;
}
if(anagram == true){
System.out.println("Strings are not Anagrams");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   check if 2 strings are Anagrams Asked in 30 Companies basic   frequent Related Questions Difference between == and .equals() ? Why is String immutable in Java ? Explain the scenerios to choose between String , StringBuilder and StringBuffer ?
or
What is the difference between String , StringBuilder and StringBuffer ? What are the difference between composition and inheritance in Java? Why Char array is preferred over String for storing password? Does garbage collection guarantee that a program will not run out of memory? Why do we need Inner classes ? Cant we just work with outer classes wherever we implement Inner classes ? What are different ways to create String Object? Explain. Explain OOPs
or
Explain OOPs Principles
or
Explain OOPs Concepts
or
Explain OOPs features
or
Tell me something about OOPs What is the difference between Encapsulation and Abstraction?