打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Mock Exam II
userphoto

2005.11.02

关注
SCJP模拟试题二

此套试题由60道题组成(实际考试为60道题)。


Questions

Question 1)

Which of the following lines will compile without warning or error.

a) float f = 1.3;
b) char c = "a";
c) byte b = 257;
d) boolean b = null;
e) int i = 10;

answer


Question 2)

What will happen if you try to compile and run the following code

public class MyClass {
public static void main(String arguments[])
{
amethod(arguments);
}
public void amethod(String[] arguments)
{
System.out.println(arguments);
System.out.println(arguments[1]);
}
}
1) error Can‘t make static reference to void amethod.
2) error method main not correct
3) error array must include parameter
4) amethod must be declared with String

answer


Question 3)

Which of the following will compile without error

1) import java.awt.*;
package Mypackage;
class Myclass {}

2) package MyPackage;
import java.awt.*;
class MyClass{}

3) /*This is a comment */
package MyPackage;
import java.awt.*;
class MyClass{}

answer


Question 4)

A byte can be of what size

1) -128 to 127
2) (-2 power 8 )-1 to 2 power 8
3) -255 to 256
4) depends on the Java Virtual machine

answer


Question 5)

What will be printed out if this code is run with the following command line
java myprog good morning

public class myprog{
public static void main(String argv[])

{
System.out.println(argv[2])
}

}

1) myprog
2) good
3) morning
4) Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2"

answer


Question 6)

Which of the following are java reserved words

1) if
2) then
3) goto
4) while
5) case

answer


Question 7)


Which of the following are legal identifiers

1) 2variable
2) variable2
3) _whatavariable
4) _3_
5) $anothervar
6) #myvar

answer


Question 8)

What will happen when you compile the following code

public class MyClass{
static int i;
public static void main(String argv[]){
System.out.println(i);
}
}

1) Error Variable i may not have been initialized
2) null
3) 1
4) 0

answer


Question 9)

What will happen if you try to compile and run the following code

public class Q {
public static void main(String argv[]){
int anar[]= new int[]{1,2,3};

System.out.println(anar[1]);
}
}

1) 1
2) Error anar is referenced before it is initialized
3) 2
4) Error size of array must be defined

answer


Question 10)

What will happen if you try to compile and run the following code

public class Q {
public static void main(String argv[]){
int anar[]= new int[5];

System.out.println(anar[0]);
}
}

1) Error: anar is referenced before it is initialized
2) null
3) 0
4) 5

answer



Question 11)

What will be the result of attempting to compile and run the following code

abstract class MineBase {
abstract void amethod();
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]);
}
}

1) a sequence of 5 0‘s will be printed
2) Error: ar is used before it is initialized
3) Error Mine must be declared abstract
4) IndexOutOfBoundes Error i

answer



Question 12)

What will be printed out if you attempt to compile and run the following code
int i = 1;
switch (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");
}

1) one
2) one, default
3) one, two, default
4) default

answer


Question 13)

What will be printed out if you attempt to compile and run the following code

int i = 9;
switch (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");
}

1) default
2) default, zero
3) error default clause not defined
4) no output displayed

answer


Question 14)

Which of the following lines of code will compile without error

1) int i=0;
if(i)
{
System.out.println("Hello");
}
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");

answer



Question 15)

What will be output if you try to compile and run the following code and and there is
no file called Hello.txt in the current directory.

import java.io.*;
public class Mine
{
public static void main(String argv[]){

Mine m = new Mine();
System.out.println(m.amethod());
}
public int amethod()
{
try {

FileInputStream dis = new FileInputStream("Hello.txt");
}catch (FileNotFoundException fne) {
System.out.println("No such file found");
return -1;
}catch(IOException ioe) {
} finally{
System.out.println("Doing finally");
}
return 0;
}
}

1) No such file found
2 No such file found ,-1
3) No such file found, doing finally, -1
4) 0

answer


Question 16)

What tags are mandatory when creating HTML to display an applet

1) name, height, width
2) code, name
3) codebase, height, width
4) code, height, width

answer


Question 17)

What will happen if you attempt to compile and run the following 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;

}

}

1) Compile and run without error
2) Compile time Exception
3) Runtime Exception

answer


Question 18)

If the following HTML code is used to display the applet in the code MgAp what will be displayed at the console.

<applet code = MgAp.class height=400 width=400 parameter HowOld=30 >
</applet>

import java.applet.*;
import java.awt.*;
public class MgAp extends Applet{

public void init(){
System.out.println(getParameter("age"));
}
}

1) Error no such parameter
2) 0
3) null
4) 30

answer


Question 19)

You are browsing the Java HTML documentation for information on the
java.awt.TextField component. You want to create Listener code to respond to focus
events. The only Listener method listed is addActionListener. How do you go about
finding out about Listener methods.

1) Define your own Listener interface according to the event to be tracked
2) Use the search facility in the HTML documentation for the listener needed
3) Move up the hierarchy in the HTML documentation to locate methods in base
classes
4) Subclass awt.event with the appropriate Listener method

answer


Question 20)

What will be displayed when you attempt to compile and run the following code

//Code start
import java.awt.*;

public class Butt extends Frame{

public static void main(String argv[]){
Butt MyBut= new Butt();
}

Butt(){
Button HelloBut = new Button("Hello");
Button ByeBut = new Button("Bye");
add(HelloBut);
add(ByeBut);
setSize(300,300);
setVisible(true);
}
}
//Code end

1) Two buttons side by side occupying all of the frame, Hello on the left and Bye on
the right
2) One button occupying the entire frame saying Hello
3) One button occupying the entire frame saying Bye
4) Two buttons at the top of the frame one saying Hello the other saying Bye

answer


Question 21-50‘s answers are here.

Question 21:

In the following code fragment from an applet, we know that the getParameter call may return a null if there is no parameter named size. Which logical operator should replace X in line 5 to ensure that a NullPointerException is not generated if tmp is null.

1. int sz;
2. public void init(){
3. sz=10;
4. String tmp=getParameter("size");
5. if(tmp!=null X tmp.equals("BIG")) sz=20;
6. }

a). Replace X with ‘&‘
b). Replace X with ‘&&‘
c). Replace X with ‘|‘
d). Replace X with ‘||‘


Question 22:
Which of the following statements results in C containing the special "Not a Number" value?

a. float C=1234.0F/0.0F;
b. float C=(float)java.lang.Math.sqrt(-1.0);
c. float C=Float.MIN_VALUE/Float.MAX_VALUE;


Question 23: [Check all correct answers]
You are writing a java class in a file named "MyClass.java", this class must be accessible by all classes in a large project. Which of the following would be correct class declarations?

a. private class MyClass extends Object
b. class myclass extends Object
c. public class MyClass
d. public class MyClass extends Object


Question 24: [Check all correct answers]
Once created, some Java objects are "immutable", meaning they can not have their contents changed. Which of the following classed produce immutable objects?

a. java.lang.Double
b. java.lang.StringBuffer
c. java.lang.Boolean
d. java.lang.Math


Question 25:
Given the following class definitions:
1. class BaseWidget extends Object{
2. String name="BaseWidget";
3. void speak(){System.out.println("I am a "+name);}
4. }
5. class TypeAWidget extends BaseWidget{
6. TypeAWidget(){name="TypeA";}
7. }

Which of the following code fragments will compile and execute without error?
a. Object A=new BaseWidget();
A.speak();
b. BaseWidget B=new TypeAWidget();
B.speak();
c. TypeAWidget C=new BaseWidget();
C.speak();


Question 26: [Check all correct answers]
Pick the keyword(s) which can NOT be used as modifiers in declaration of a method in a Java class.

a) private
b) friend
c) protected
d) static
e) synchronized
f) generic

 


Question 27: [Check all correct answers]
Which of the following would be an illegal identifier for a Java method?

a) do_it_now
b) _Substitute
c) 9thMethod
d) $addMoney
e) %getPath

 


Question 28:
Given the following code for the Demo class:

public class Demo{
private int[] count;
public Demo(){ count=new int[10];}
public void setCount(int ct,int n){ count[n]=ct;}
public void showCount(int n){
System.out.println("Count is "+count[n]);
}
public int getCount(int n){ return count[n];}
}

what would be the result of calling the showCount method with a parameter of 9 immediately after creating an instance of Demo?

a) A NullPointerException would be thrown, halting the program.
b) Standard output would show "Count is 0".
c) An ArrayIndexOutOfBoundsException would be thrown, halting the program.
d) Standard output would show "Count is null".

 


Question 29:

What happens when we attempt to compile and run the following code?
1. public class Logic{
2. static long sixteen=0x0010;
3. static public void main(String args[]){
4. long N=sixteen>>4:
5. System.out.println("N= "+N);
6. }
7. }

a) The compiler will object to line 4 combining a long with an int.
b) The program will compile and run, producing the output "N=0".
c) The program will compile and run, producing the output "N=1".
d) A rutime exception will be thrown.

 


Question 30:
What will happen on trying to compile and run the following application?
1. public class Example{
2. public Boolean flags[]=new Boolean[4];
3. public static void main(String[] args){
4. Example E=new Example();
5. System.out.println("Flag 1 is "+E.flags[1]);
6. }
7. }

a) The text "Flag 1 is true" will be written to standard output.
b) The text "Flag 1 is false" will be written to standard output.
c) The text "Flag 1 is null" will be written to standard output.
d) The compiler will object to line 2.

 


Question 31: [Check all correct answers]

Which of the following code fragments are legal Java code?

a) String A="abcdefg";
A-="cde";
b) String A="abcdefg";
A+="cde";
c) Integer J=new Integer(27);
J-=7;
d) Integer J=new Integer(27);
J--;

 


Question 32:

Given the following code for the code for Demo class, where "XXXX" represents an access modifier:

public class Demo extends Base{
XXXX String userName;
public void setName(String s){ userName=s;}
public void showName(){
System.out.println("Name is "+userName):
}
public String getName(){ return userName; }
}

Select the modifier which would be used to give only classes in the default package or classes derived from Demo, direct access to the userName String variable.

a) public
b) blank (ie - the line would read "String userName :")
c) protected
d) private


Question 33:

Given the following method in an application:

1. public String setFiletype(String fname){
2. int p=fname.indexOf(‘.‘);
3. if(p>0) fname=fname.substring(0,p);
4. fname+=".TXT";
5. return fname;
6. }

and given that another part of the class has a the following code:
7. String TheFile="Program.java";
8. File F=new File(setFileType(TheFile));
9. System.out.println("Created "+TheFile);
What will be printed by the statement in line 9?

a) "Created Program.java"
b) "Created Program.txt"
c) "Created Program.java.txt"


Question 34:

What happens on trying to compile and run the following code?
1. public class EqualsTest{
2. public static void main(String args[]){
3. byte A=(byte)4096;
4. if(A==4096)System.out.println("Equal");
5. else System.out.println("Not Equal");
6. }
7. }

a) The compiler objects to the loss of accurcy in the cast in line 3.
b) The program compiles and prints "Not Equal".
c) The program compiles and prints "Equal".


Question 35:

What happens on trying to compile and run the following code?

1. public class EqualsTest{
2. public static void main(String args[]){
3. Long LA=new Long(7);
4. Long LB=new Long(7);
5. if(LA==LB) System.out.println("Equal");
6. else System.out.println("Not Equal");
7. }
8. }

a) The program compiles but throws a runtime exception in line 5.
b) The program compiles and prints "Not Equal";
c) The program compiles and prints "Equal".


Question 36: [Check all correct answers]

Given the following code for the Demo class:

public class Demo extends Base{
private int count;
public Demo(){
System.out.println("A Demo object has been created");
}
protected void addOne() {count++; }
}

Which of the following statements about the "count" variable is correct?

a) When a new Demo object is created, the value of count is zero.
b) When a new Demo object is created, the value of count is undefined.
c) An object of the Base class can have methods which modify the count variable.
d) The only way the count variable can be modified is by calling the addOne method.


Question 37: [Check all correct answers]

Which of the following statements will cause a compiler error.

a) float F=4096.0;
b) double D=4096.0;
c) byte B=4096;
d) char C=4096;


Question 38:

In the following method, which may be called with any kind of Object, we want to "short circuit" the logical test in line 2 if the object is not a Long. Which logical operator should replace the X in line 2 to accomplish this?

1. long Test(Object ob){
2. if(Ob instanceof Long X ((Long)Ob).longValue()>999){
3. return((Long)Ob).longValue();
4. }
5. return -1L;
6. }

a) Replace ‘X‘ with ‘&&‘.
b) Replace ‘X‘ with ‘||‘.
c) Replace ‘X‘ with ‘&‘.
d) Replace ‘X‘ with ‘|‘.


Question 39:

Which of the following statements is the correct form fro determining whether the float primitive X has the specail "Not a Number" value?

a) if(X instanceof Float.NaN)
b) if(X==Float.NaN)
c) if(Float.isNaN(X))

 


Question 40:

Which of the following are organizing principles of Java‘s "javadoc" format documentation for a given class?

a) Relative frequency of use of the various methods.
b) Lists of methods in a class in alphabetic order.
c) Separate listing of private, protected and public methods in a calss.

 


Question 41:

The following program is compiled and then run with this command line:

java Demo alpha beta gamma

public class Demo{
public static void main(String args[]

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java学习代码,分享史上java最牛逼,最简短的代码
Java第一次作业
最全的Java笔试题库之选择题篇-总共234道【1~60】
《Java 程序设计》模拟试题
java基础习题(一)
Java中static、this、super、final用法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服