New Java
New Java
New Java
9)class X
{
public static void main(String[] args)
{
int x=9;
if(x==9){
int x=8;
System.out.println(X);
}
}}
http://www.sanfoundry.com/java-mcqs-class-fundamental-declaring-objects/
ans compilation error
11).
Which statement is true?
A. Memory is reclaimed by calling Runtime.gc().
B. Objects are not collected if they are accessible from live threads.
C. An OutOfMemory error is only thrown if a single block of memory cannot be
found that is large enough for a particular requirement.
D. Objects that have finalize() methods always have their finalize() methods
called before the program ends.
http://www.indiabix.com/java-programming/garbage-collections/021001
Answer: Option B
12)
what is the output of this program?
class static_out{
static int x;
static int y;
void add(int a,int b){
x=a+b;
y=x+b;
}
}
class static_use{
public static void main(String[] args)
{
staic_out obj1=new static_out();
staic_out obj2=new static_out();
int a=2;
obj1.add(a,a+1);
obj2.add(5,a);
System.out.println(obj1.x+""+obj2.y)
}
}
ans 79
http://www.techepi.com/online-test/java/low2/high/.html
http://www.studytonight.com/java/tests/12
15 )class Boo
{
Boo(String s) { }
Boo() { }
}
class Bar extends Boo
{
Bar() { }
Bar(String s) {super(s);}
void zoo()
{
// insert code here
}
}
a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
View Answer
http://www.sanfoundry.com/java-mcqs-isalive-join-thread-synchronization/
Answer: b
class string_class {
public static void main(String args[])
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
obj2 = " world";
System.out.println(obj + " " + obj2);
}
}
advertisements
a) hello hello
b) world world
c) hello world
d) world hello
View Answer
http://www.sanfoundry.com/java-mcqs-string-class/
Answer: c
import java.io.*;
class streams {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeFloat(3.5);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
float x;
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
x=ois.readInt();
ois.close();
System.out.println(x);
}
catch (Exception e) {
System.out.print("deserialization");
System.exit(0);
}
}
}
ans New input stream is linked to streal serials, an object ois of
ObjectInputStream is used to access this newly created stream, ois.close(); closes
the stream hence we cant access the stream and ois.available() returns 0.
Output:
javac streams.java
java streams
0
class multithreaded_programing{
public static void main(String args[]){
new newthread();
}}
class newthread implements Runnable {
Thread t;
newthread() {
t1 = new Thread(this,"Thread_1");
t2 = new Thread(this,"Thread_2");
t1.start();
t2.start();
}
public void run() {
t2.setPriority(Thread.MAX_PRIORITY);
System.out.print(t1.equals(t2));
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
a) true
b) false
c) truetrue
d) falsefalse
View Answer
http://www.sanfoundry.com/java-mcqs-creating-threads/
Answer: d
Explanation: Threads t1 & t2 are created by class newthread that is implementing
runnable interface,
hence both the threads are provided their own run() method specifying the actions
to be taken.
When constructor of newthread class is called first the run() method of t1
executes than the run method of t2 printing 2 times
false as both the threads are not equal one is having different priority than
other, hence falsefalse is printed.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse