Replies
Welcome, guest
Join CrazyEngineers to reply, ask questions, and participate in conversations.
CrazyEngineers powered by Jatra Community Platform
-
@thebigk • Sep 14, 2009
The questions seem to be very basic and answers can be found in any JAVA book. Instead of asking others to solve it for you, why don't you post your answers and ask others to verify? -
@sookie-T06sFW • Sep 14, 2009
Hi ,
Following are the answers of objective questions in those two exams. Feel free to correct if any is wrong.
Q1 Which class has implicit instantiation?
A: String [Not sure]. Even if we don't use new keyword, their object is created on the heap.
Q2: How method overriding can be prevented?
A: Marking the method with "final" keyword
Q3:Which class is immutable?
A: String
Q4: If you write System.exit(0); at the end of the try block , will the finally block execute?
A: No, System.exit(0); terminates the execution of the thread in which it is called.
Q5: Can a Byte object be cast to a double value?
A: In Java1.5 onwards, it don't need any casting because of autoboxing but in earlier versions it cannot be casted.
Q6: Which of the following are legal identifiers?
a) 2variable ->No, cannot start with a number
b) variable2 ->Yes
c) _whatavariable- >Yes
d)_3_ ->Yes
7: True/False [Not sure]
a) Methods cannot be overridden to be more private.
False. Access level can be more restrictive than the overridden method.
b) static methods cannot be overloaded .
False. They are rather called as redefined instead of calling as overloaded.
c) private methods cannot be overloaded.
False.
d) An overloaded method cannot throw exceptions not checked in the base class.
False. It can throw new checked exceptions
Q8. What is the base class of all classes?
A: Object
Q9: Which package is imported by default?
A: java.lang
Q10: What is the size of char primitive datatype in Java?
A: 16 bits or 2 bytes
Q11: What happens if you define a static variable within a method of class?
A: Compile time error -"Illegal start of expression" . They should be marked inside the class as they are shared by the class irrespective of instances.
Q12: An abstract class can be instantiated?
A: False. Its purpose is to get extended.
Q13: Arrays are primitive datatypes?
A: False. int, char, booleanetc. etc are primitive data-types.
Q14:A main method can be overload?
A: True.
Q15: A class can be declared as static?
A: True.Nested classes can be declared as static.
Q16:A method declared inside an Interface can be declared as final?
A: False. No because they are implicitly "abstract".A method cannot be both "abstract" and "final".
Q17:An interface implements another interface?
A: False.An interface extends another interface
Q18: A class can be defined inside an interface?
A: True [Not sure]
Q19: An interface can be defined inside a class?
A: True [Not sure]
Q20: To what value is a value of the variable of String type automatially initialized?
A: null
Q21: What is the defualt priority of a thread?
A: 5 ->NORM_PRIORITY
Q22:What is similarity/differences between Abstract classes and Interfaces?
A:Similarity: Both cannot be instantiated. Both contains at least one abstract method.
Difference: Abstract class can define both abstract and non-abstract methods but an interface can have only abstract methods.
Q23:What is the difference between checked and unchecked exceptions?
A: Checked exceptions are checked by compiler whether the method handles or throws these exceptions while unchecked exceptions are Runtime exceptions that are not checked at compile time.
Q24:What is the difference between Multi-threading and multi-processing?
A:Multi-processing: It is that type of multi-tasking in which several programs(or processes) execute simultaneously.
Multi-threading: It is that type of multi-processing in which several threads (each a part of a process/program) executes simultaneously.
Multi-threading can be called as a subset of Multi-processing.
Q25:What is interpreter and compiler in java?
A: Compiler in Java is a program that converts Java source code into Java byte codes.
Interpreter in Java is a program that is a part of JVM and executes Java byte codes generated by Java compiler.
Q26:What is the benefit of multi-threading?
A:Multi-threading is used for writing efficient programs that can make maximum use of CPU so that its idle time can be used in optimum way. It is useful in networked environment where many requests are made simultaneously. -
@sookie-T06sFW • Sep 14, 2009
Q27: Find out the errors in the following program and correct them.
public class Test { float x = y; byte b = 51; static int y = 10; void setData() { b = b * 2; } public static void main(String[] args) { Test.setData(); return; System.out.println("ABC" + x); } }A: Errors are:
1. Compile time error are b=b*2; because byte * byte gives int and int cannot be directly assigned to byte without explicit casting.
2. Compile time error at Test.setData(); and System.out.println("ABC" + x);because setData() method is non-static and non static variables or methods cannot be called in static methods without object reference.
3. return; should always be last statement. So it should come after System.out.println("ABC" + x);
Corrected program:
public class Test { float x = y; byte b = 51; static int y = 10; void setData() { b = (byte) (b * 2); } public static void main(String[] args) { Test t = new Test(); t.setData(); System.out.println("ABC" + t.x); return; } } -
@sookie-T06sFW • Sep 14, 2009
Q28: Write a program to show the use of import and package statements.
A: Make two folders in any drive of your system
a) Name one as "com"
b) Name other as "org"
In "org" folder, create a following ImportInHello.java class. Add "package org;" at the top of the file.
package org; public class ImportInHello{ public void printHello(){ System.out.println("Hello in org package"); } }Now come to "com" folder,create a class named as "Hello.java" which will import class ImportInHello.java
package com; import org.ImportInHello ; public class Hello{ public static void main(String[] args){ System.out.println("Hello in com package"); ImportInHello obj = new ImportInHello(); obj.printHello(); } }Now compile both the files are run "Hello.java", following output will be shown
Hello in com package
Hello in org package -
@thebigk • Sep 14, 2009
Sookie - you rock! -
@thebigk • Sep 15, 2009
@sarveshgupta: Maybe you should take a moment to thank sookie? -
@sahithi-oJZaYj • Sep 15, 2009
-
@gauravbhorkar-Pf9kZD • Sep 15, 2009
Sookie, you rock ! -
@sarveshgupta-txtmu5 • Sep 16, 2009
Hey Sookie thanks a lot
@Biggie : I myself really feel thankful to Sookie Although I was not able to see these before my exam today only I have logged in
But still really thanks a lot Sookie for taking out your precious time to solve these queries
It will surely help in future
Great work -
@thebigk • Sep 16, 2009
-
@sookie-T06sFW • Sep 16, 2009
Thanks everyone, well I was expecting that by this time someone would have posted the answers of the questions which I left for others. Theoretical questions and too easy to answer. Just because of time consuming I left them at that moment. Later, I will do so.
Thanks !