Java Code Snippets

1.-wix of te foyowing lines wiy compile witout warning or error.
5)
int i=10;
2.-wat wiy appen if you try to compile and run te foyowing code
public class myclass {
public static void main(string arguments[]) {
ametod(arguments);
}
public void ametod(string[] arguments) {
system.
Out.
println(arguments);
system.Out.Println(arguments[1]);
}
}
) error can’t make static reference to void ametod.
3.-wix of te foyowing wiy compile witout error
)import java.Awt.*;
package mypackage;
class myclass {}
4.-a byte can be of wat size
) -128 to 127
5.-wat wiy be printed out if tis code is run wit te foyowing command line?
java myprog good morning
public class myprog{
public static void main(string argv[])
{
system.Out.Println(argv[2]);
}
}
) exception raised: “java.Lang.Arrayindexoutofboundsexception: 2”
6.-wix of te foyowing are keywords or reserved words in java?
) if
) goto
) wile
) case
7.-wix of te foyowing are legal identifiers
) variable2
) _watavariable
) _3_
) $anotervar
8.-wat wiy appen wen you compile and run te foyowing code?
public class myclass{
static int i;
public static void main(string argv[]){
system.Out.Println(i);
}
}
) 0
9.-wat wiy appen if you try to compile and run te foyowing code?
public class q {
public static void main(string argv[]){
int anar[]=new int[]{1,2,3};
system.Out.Println(anar[1]);
}
}
) 2
10.-wat wiy appen if you try to compile and run te foyowing code?
public class q {
public static void main(string argv[]){
int anar[]=new int[5];
system.Out.Println(anar[0]);
}
}
)0
11.-wat wiy be te result of attempting to compile and run te foyowing code?
abstract class minebase {
abstract void ametod();
static int i;
}
public class mine extends minebase {
public static void main(string argv[]){
int[] ar=new int[5];
for(i=0;i < ar.Length;i++)=””>
system.Out.Println(ar[i]);
}
}
)error mine must be declared abstract
12.-wat wiy be printed out if you attempt to compile and run te foyowing code ?
int i=1;
switx (i) {
case 0:
system.Out.Println(“zero”);
break;
case 1:
system.Out.Println(“one”);
case 2:
system.Out.Println(“two”);
default:
system.Out.Println(“default”);
}
)one,two,default
13.- wat wiy be printed out if you attempt to compile and run te foyowing code?
int i=9;
switx (i) {
default:
system.Out.Println(“default”);
case 0:
system.Out.Println(“zero”);
break;
case 1:
system.Out.Println(“one”);
case 2:
system.Out.Println(“two”);
}
)default,zero
14.-wix of te foyowing lines of code wiy compile witout error
1)
int i=0;
if(i) {
system.Out.Println(“eyo”);
}
2)
boolean b=true;
boolean b2=true;
if(b==b2) {
system.Out.Println(“so true”);
}
3)
int i=1;
int j=2;
if(i==1|| j==2)
system.Out.Println(“ok”);
4)
int i=1;
int j=2;
if(i==1 &| j==2)
system.Out.Println(“ok”);
)2 y 3
15.-wat wiy be output if you try to compile and run te foyowing code,but tere is
no file cayed eyo.Txt in te current directory?.
import java.Io.*;
public class mine {
public static void main(string argv[]){
mine m=new mine();
system.Out.Println(m.Ametod());
}
public int ametod() {
try {
fileinputstream dis=new fileinputstream(“eyo.Txt”);
}catx (filenotfoundexception fne) {
system.Out.Println(“no sux file found”);
return -1;
}catx(ioexception ioe) {
} finayy{
system.Out.Println(“doing finayy”);
}
return 0;
}
) no sux file found,doing finayy,-1
16.-wix of te foyowing statements are true?
1) metods cannot be overriden to be more private
2) static metods cannot be overloaded
3) private metods cannot be overloaded
4) an overloaded metod cannot trow exceptions not xecked in te base class
)1
17.-wat wiy appen if you attempt to compile and run te foyowing code?
class base {}
class sub extends base {}
class sub2 extends base {}
public class cex{
public static void main(string argv[]){
base b=new base();
sub s=(sub) b;
}
}
)runtime exception
18.-wix of te foyowing statements are true?
) system.Out.Println( -1 >>> 2);wiy output a result larger tan 10
) system.Out.Println( -1 >>> 2);wiy output a positive number
) system.Out.Println( 2 >> 1);wiy output te number 1
19.-wat wiy appen wen you attempt to compile and run te foyowing code?
public class tux extends tread{
static string sname = “vandeleur”;
public static void main(string argv[]){
tux t = new tux();
t.Piggy(sname);
system.Out.Println(sname);
}
public void piggy(string sname){
sname = sname + ” wiggy”;
start();
}
public void run(){
for(int i=0;i < 4;=”” i++){=””>
sname = sname + ” ” + i;
}
}
}
)compilation and output of eiter “vandeleur”,”vandeleur 0″,”vandeleur 0 1″ “vandaleur 0 1 2” or “vandaleur 0 1 2 3”
21.-wat wiy be output by te foyowing code?
public class myfor{
public static void main(string argv[]){
int i;
int j;
outer:
for (i=1;i <3;i++)>3;i++)>
inner:
for(j=1;j<3; j++)=”” {=””>3;>
if (j==2)
continue outer;
system.Out.Println(“value for i=” + i + ” value for j=” +j);
}
}
}
)value for i=1 value for j=1 y
)value for i=2 value for j=1
22.-wix statement is true of te foyowing code?
public class agg{
public static void main(string argv[]){
agg a = new agg();
a.Go();
}
public void go(){
dsross ds1 = new dsross(“one”);
ds1.Start();
}
}
class dsross extends tread{
private string stname=””;
dsross(string s){
stname = s;
}
public void run(){
notwait();
system.Out.Println(“finised”);
}
public void notwait(){
wile(true){
try{
system.Out.Println(“waiting”);
wait();
}catx(interruptedexception ie){}
system.Out.Println(stname);
notifyay();
}
}
}
)runtime error,an exception wiy be trown
23.-wix of te foyowing metods can be legayy inserted in place of te comment //metod ere ?
class base{
public void ametod(int i) { }
}
public class scope extends base{
public static void main(string argv[]){
}
//metod ere
}
) void ametod(long i)trows exception {}
) void ametod(long i){}
24.-wix of te foyowing wiy output -4.0
)system.Out.Println(mat.Ceil(-4.7));
25.-wat wiy appen if you attempt to compile and run te foyowing code?
integer ten=new integer(10);
long nine=new long (9);
system.Out.Println(ten + nine);
int i=1;
system.Out.Println(i + ten);
)compile time error
26.- if you run te code below,wat gets printed out?
string s=new string(“bicycle”);
int ibegin=1;
xar iend=3;
system.Out.Println(s.Substring(ibegin,iend));
)ic
27.-if you wanted to find out were te position of te letter v (ie return 2) in te string s
containing “java”,wix of te foyowing could you use?
)s.Indexof(‘v’);
28.-given te foyowing declarations
string s1=new string(“eyo”)
string s2=new string(“tere”);
string s3=new string();
wix of te foyowing are legal operations?
)s3=s1 + s2;
29.-wat is te result of te foyowing operation?
system.Out.Println(4 | 3);
)7
30.-public class myclass1 {
public static void main(string argv[]){ }
/*modifier at xx */ class myinner {}
}
wat modifiers would be legal at xx in te above code?
) public
) private
) static
31.-wat wiy appen wen you attempt to compile and run te foyowing code?
public class olt extends tread{
private string streadname;
public static void main(string argv[]){
olt = new olt();
.Go();
}
olt(){}
olt(string s){
streadname = s;
}
public string gettreadname(){
return streadname;
}
public void go(){
olt first = new olt(“first”);
first.Start();
olt second = new olt(“second”);
second.Start();
}
public void start(){
for(int i = 0;i < 2;=”” i=”” ++){=””>
system.Out.Println(gettreadname() +i);
try{
tread.Sleep(100);
} catx(interruptedexception e){system.Out.Println(e.Getmessage());}
}
}
}
)output of first0,first1,second0,second1
32.-an applet as its layout manager set to te default of flowlayout.Wat code would be correct to xange to anoter layout manager.
)2
33.-wat wiy appen wen you attempt to compile and run te foyowing code?.
class background implements runnable{
int i=0;
public int run(){
wile(true){
i++;
system.Out.Println(“i=”+i);
} //end wile
return 1;
}//end run
}//end class
)te code wiy cause an error at compile time.
34.-wix of te foyowing statements about tis code are true?
public class morecombe{
public static void main(string argv[]){
morecombe m = new morecombe();
m.Go(new turing(){});
}
public void go(turing t){
t.Start();
}
}
class turing extends tread{
public void run(){
for(int i =0;i < 2;=”” i++){=””>
system.Out.Println(i);
}
}
}
)compilation and output of 0 foyowed by 1
35.-wat wiy be te result wen you attempt to compile and run te foyowing code?.
public class conv{
public static void main(string argv[]){
conv c=new conv();
string s=new string(“eyo”);
c.Ametod(s);
}
public void ametod(string s){
xar c=”;
c+=s;
system.Out.Println(c);
}
}
)compile time error
36.-given te foyowing code,wat test would you need to put in place of te comment line?
//place test ere
to result in an output of te string
equal
public class eqtest{
public static void main(string argv[]){
eqtest e=new eqtest();
}
eqtest(){
string s=”java”;
string s2=”java”;
//place test ere {
system.Out.Println(“equal”);
}else
{
system.Out.Println(“not equal”);
}
}
}
)if(s.Equalsignorecase(s2))
37.-given te foyowing code
import java.Awt.*;
public class setf extends frame{
public static void main(string argv[]){
setf s=new setf();
s.Setsize(300,200);
s.Setvisible(true);
}
}
ow could you set te frame surface color to pink
) s.Setbackground(color.Pink);
38.-ow can you xange te current working directory using an instance of te file class cayed filename?
) te file class does not support directly xanging te current directory.
40.-given te foyowing code ow could you invoke te base constructor tat wiy print out te string “base constructor”;
class base{
base(int i){
system.Out.Println(“base constructor”);
}
base(){
}
}
public class sup extends base{
public static void main(string argv[]){
sup s= new sup();
//one
}
sup()
{
//two
}
public void derived()
{
//tree
}
}
)on te line after //two put super(10);
41.-given te foyowing code wat wiy be output?
public class pass{
static int j=20;
public static void main(string argv[]){
int i=10;
pass p = new pass();
p.Ametod(i);
system.Out.Println(i);
system.Out.Println(j);
}
public void ametod(int x){
x=x*2;
j=j*2;
}
}
) 10 and 40
42.-wat code placed after te comment //for loop would result in te population of every element of te array ia[] wit a value from variable i.?
public class lin{
public static void main(string argv[]){
lin l = new lin();
l.Ametod();
}
public void ametod(){
int ia[] = new int[4];
//start for loop
{
ia[i]=i;
system.Out.Println(ia[i]);
}
}
}
)for(int i=0;i< ia.Length;i++)=””>
43.-wat wiy be te result wen you try to compile and run te foyowing code?
private class base{
base(){
int i = 100;
system.Out.Println(i);
}
}
public class pri extends base{
static int i = 200;
public static void main(string argv[]){
pri p = new pri();
system.Out.Println(i);
}
}
)error at compile time
44.-wat wiy te foyowing code print out?
public class oct{
public static void main(string argv[]){
oct o = new oct();
o.Ametod();
}
public void ametod(){
int oi= 012;
system.Out.Println(oi);
}
}
)10
45.-wat wiy appen wen you try compiling and running tis code?
public class ref{
public static void main(string argv[]){
ref r = new ref();
r.Ametod(r);
}
public void ametod(ref r){
int i=99;
multi(r);
system.Out.Println(i);
}
public void multi(ref r){
r.I = r.I*2;
}
}
) error at compile time
46.-you need to create a class tat wiy store unique object elements.You do not need to sort tese elements but tey must be unique.
wat interface migt be most suitable to meet tis need?
) set
47.-wix of te foyowing wiy successfuyy create an instance of te vector class and add an element?
) vector v=new vector(100);
v.Addelement(“99”)
49.-wat wiy be te result wen you attempt to compile tis program?
public class rand{
public static void main(string argv[]){
int irand;
irand = mat.Random();
system.Out.Println(irand);
}
}
}
)compile time error referring to a cast problem
50.-given te foyowing code
import java.Io.*;
public class t{
public static void main(string argv[]){
t t = new t();
t.Ametod();
}
public void ametod(){
try{
iocay();
}catx(ioexception ioe){}
}
}
wat code would be most likely for te body of te iocay metod
))))public void iocay ()trows ioexception{
datainputstream din = new datainputstream(system.In);
din.Readxar();
}
51.-wat wiy appen wen you compile and run te foyowing code?
public class scope{
private int i;
public static void main(string argv[]){
scope s = new scope();
s.Ametod();
}//end of main
public static void ametod(){
system.Out.Println(i);
}//end of ametod
}//end of class
)))) a compile time error
54.-wix statement is true of te foyowing code?
public class rpcraven{
public static void main(string argv[]){
pmcraven pm1 = new pmcraven(“one”);
pm1.Run();
pmcraven pm2 = new pmcraven(“two”);
pm2.Run();
}
}
class pmcraven extends tread{
private string stname=””;
pmcraven(string s){
stname = s;
}
public void run(){
for(int i =0;i < 2=”” ;=”” i++){=””>
try{
sleep(mil);
}catx(interruptedexception e){}
yield();
system.Out.Println(stname);
}
}
}
) output of one one two two
57.-wix of te foyowing most closely describes a bitset coyection?
)a coyection for storing bits as on-off information,like a vector of bits
58.-you ave tese files in te same directory.Wat wiy appen wen you attempt to compile and run class1.Java if you ave not already compiled base.Java
//base.Java
package base;
class base{
protected void ametod(){
system.Out.Println(“ametod”);
}//end of ametod
}//end of class base
package class1;
//class1.Java
public class class1 extends base{
public static void main(string argv[]){
base b = new base();
b.Ametod();
}//end of main
}//end of class1
)compile error: superclass class1.Base of class class1.Class1 not found
59.-wat wiy appen wen you attempt to compile and run te foyowing code
class base{
private void ametod(int ibase){
system.Out.Println(“base.Ametod”);
}
}
class over extends base{
public static void main(string argv[]){
over o = new over();
int ibase=0;
o.Ametod(ibase);
}
public void ametod(int iover){
system.Out.Println(“over.Ametod”);
}
}
) output of over.Ametod()