java试题
Posted 思否学否
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java试题相关的知识,希望对你有一定的参考价值。
Module 1 – Getting Started
Q1.What will happen when you compile and run 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
Q2.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{}
Q3.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
Q4.Given the following code
public class Sytch{
int x=2000;
public static void main(String argv[]){
System.out.println("Ms "+argv[1]+"Please pay $"+x);
}
}
What will happen if you attempt to compile and run this code with the command line
java Sytch Jones Diggle
1) Compilation and output of Ms Diggle Please pay $2000
2) Compile time error
3) Compilation and output of Ms Jones Please pay $2000
4) Compilation but runtime error
Q5.You have a public class called myclass with the main method defined as follows
public static void main(String parm[]){
System.out.println(parm[0]);
}
If you attempt to compile the class and run the program as follows
java myclass hello
What will happen?
1) Compile time error, main is not correctly defined
2) Run time error, main is not correctly defined
3) Compilation and output of java
4) Compilation and output of hello
Module 2 Identifiers, Keywords, and Types
Q1.Which of the following are legal statements?
1) float f=1/3;
2) int i=1/3;
3) float f=1.01;
4) double d=999d;
Q2.Which of the following are Java keywords?
1) NULL
2) new
3) instanceOf
4) wend
Q3.Which of the following are Java keywords?
1) sizeof
2) main
3) transient
4) volatile
Q4.Given the following declaration
Integer i=new Integer(99);
How can you now set the value of i to 10?
1) i=10;
2) i.setValue(10);
3) i.parseInt(10);
4) none of the above
Q5.Which of the following will compile correctly
1) short myshort = 99S;
2) String name = ‘Excellent tutorial Mr Green‘;
3) char c = 17c;
4)int z = 015;
Q6.Which of the following are Java key words
1)double
2)Switch
3)then
4)instanceof
Q7.Which of the following lines will compile without warning or error.
1) float f=1.3;
2) char c="a";
3) byte b=257;
4) boolean b=null;
5) int i=10;
Q8.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 particular implementation of the Java Virtual machine
Q9.Which of the following are keywords or reserved words in Java?
1) if
2) then
3) goto
4) while
5) case
Q10.Which of the following are legal identifiers
1) 2variable
2) variable2
3) _whatavariable
4) _3_
5) $anothervar
6) #myvar
Module 4 – Arrays
Q1.What code placed after the comment //For loop would result in the population of every element of the array ia[] with a value from variable i.?
public class Lin{
public static void main(String argv[]){
Lin l = new Lin();
l.amethod();
}
public void amethod(){
int ia[] = new int[4];
//Start For loop
{
ia[i]=i;
System.out.println(ia[i]);
}
}
}
1) for(int i=0; i < ia.length() -1; i++)
2) for (int i=0; i< ia.length(); i++)
3) for(int i=1; i < 4; i++)
4) for(int i=0; i< ia.length;i++)
Q2.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
Q3.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"
Q4.You want to find out the value of the last element of an array. You write the following code. What will happen when you compile and run it.?
public class MyAr{
public static void main(String argv[]){
int[] i = new int[5];
System.out.println(i[5]);
}
}
1) An error at compile time
2) An error at run time
3) The value 0 will be output
4) The string "null" will be output
Q5.Which of the following statements are true?
1) The elements in a Java array can only be of primitive types, not objects
2) Arrays elements are initialized to default values wherever they are created using the keword new
3) An array may be dynamically resized using the setSize method
4) You can find out the size of an array using the size method
Module 3 – Expressions and Flow Control
Q1.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
Q2.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
Q3.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");
Q4.Which of the following statements are true?
1) System.out.println( -1 >>> 2);will output a result larger than 10
2) System.out.println( -1 >>> 2); will output a positive number
3) System.out.println( 2 >> 1); will output the number 1
4) System.out.println( 1 <<< 2); will output the number 4
Q5.What will happen if you attempt to compile and run the following code?
Integer ten=new Integer(10);
Long nine=new Long (9);
System.out.println(ten + nine);
int i=1;
System.out.println(i + ten);
1) 19 followed by 20
2) 19 followed by 11
3) Compile time error
4) 10 followed by 1
Given the following declarations
String s1=new String("Hello")
String s2=new String("there");
String s3=new String();
Q6.Which of the following are legal operations?
1) s3=s1 + s2;
2) s3=s1-s2;
3) s3=s1 & s2;
4) s3=s1 && s2
Q7.What is the result of the following operation?
System.out.println(4 | 3);
1) 6
2) 0
3) 1
4) 7
Q8.What will happen when you attempt to compile and run the following code
public class MySwitch{
public static void main(String argv[]){
MySwitch ms= new MySwitch();
ms.amethod();
}
public void amethod(){
int k=10;
switch(k){
default: //Put the default at the bottom, not here
System.out.println("This is the default output");
break;
case 10:
System.out.println("ten");
case 20:
System.out.println("twenty");
break;
}
}
}
1) None of these options
2) Compile time error target of switch must be an integral type
3) Compile and run with output "This is the default output"
4) Compile and run with output of the single line "ten"
Q9.What will happen when you attempt to compile and run the following code
int Output=10;
boolean b1 = false;
if((b1==true) && ((Output+=10)==20)){
System.out.println("We are equal "+Output);
}else
{
System.out.println("Not equal! "+Output);
}
1) Compile error, attempting to peform binary comparison on logical data type
2) Compilation and output of "We are equal 10"
3) Compilation and output of "Not equal! 20"
4) Compilation and output of "Not equal! 10"
Q10.Given the following variables which of the following lines will compile without error?
String s = "Hello";
long l = 99;
double d = 1.11;
int i = 1;
int j = 0;
1) j= i <<s;
2) j= i<<j;
3) j=i<<d;
4)j=i<<l;
Q11.What will be output by the following line of code?
System.out.println(010|4);
1) 14
2) 0
3) 6
4) 12
Given the following variables
char c = ‘c‘;
int i = 10;
double d = 10;
long l = 1;
String s = "Hello";
Q12.Which of the following will compile without error?
1)c=c+i;
2)s+=i;
3)i+=s;
4)c+=s;
Q13.Which of the following will compile without error?
1)
char c=‘1‘;
System.out.println(c>>1);
2)
Integer i=Integer("1");
System.out.println(i>>1);
3)
int i=1;
System.out.println(i<<<1);
4)
int i=1;
System.out.println(i<<1);
Q14.Which of the following statements are true?
1) The String class is implemented as a char array, elements are addressed using the stringname[] convention
2) The + operator is overloaded for concatenation for the String class
3) Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type
4) The size of a string can be retrieved using the length property
Q15.Which of the following statements are true?
1) The following statement will produce a result of 1. System.out.println( -1 >>>2);
2) Performing an unsigned left shift (<<<) on a negative number will always produce a negative number result
3) The following statement will produce a result of zero, System.out.println(1 >>1);
4) All the Java integral types are signed numbers
Q16. Given the following class
public class Ombersley{
public static void main(String argv[]){
boolean b1 = true;
if((b1 ==true) || place(true)){
System.out.println("Hello Crowle");
}
}
public static boolean place(boolean location){
if(location==true){
System.out.println("Borcetshire");
}
System.out.println("Powick");
return true;
}
}
What will happen when you attempt to compile and run it?
1) Compile time error
2) Output of "Hello Crowle"
3) Output of Borcetshire and Powick followed by "Hello Crowle"
4) No output
Q17. Which of the following statements are true?
1) The % is used to calculate a percentage thus: 10 % 20=50
2) The / operator is used to divide one value by another
3) The # symbol may not be used as the first character of a variable
4) The $ symbol may not be used as the first character of a variable
Q18.What will happen when you attempt to compile and run the following code?
public class Agg{
static public long i=10;
public static void main(String argv[]){
switch(i){
default:
System.out.println("no value given");
case 1:
System.out.println("one");
case 10:
System.out.println("ten");
case 5:
System.out.println("five");
}
}
}
1) Compile time error
2) Output of "ten" followed by "five"
3) Output of "ten"
4) Compilation and run time error because of location of default
Q19.What will happen when you attempt to compile and run the following code
public class StrEq{
public static void main(String argv[]){
StrEq s = new StrEq();
}
private StrEq(){
String s = "Marcus";
String s2 = new String("Marcus");
if(s == s2){
System.out.println("we have a match");
}else{
System.out.println("Not equal");
}
}
}
1) Compile time error caused by private constructor
2) Output of "we have a match"
3) Output of "Not equal"
4) Compile time error by attempting to compare strings using ==
Q20.Given the following code, what test would you need to put in place of the comment line?
//place test here
to result in an output of the string
Equal
public class EqTest{
public static void main(String argv[]){
EqTest e=new EqTest();
}
EqTest(){
String s="Java";
String s2="java";
//place test here {
System.out.println("Equal");
}else
{
System.out.println("Not equal");
}
}
}
1) if(s==s2)
2) if(s.equals(s2)
3) if(s.equalsIgnoreCase(s2))
4)if(s.noCaseMatch(s2))
Module 5 – Objects and Classes
Q1.Given the following code
class Base {}
class Agg extends Base{
public String getFields(){
String name = "Agg";
return name;
}
}
public class Avf{
public static void main(String argv[]){
Base a = new Agg();
//Here
}
}
What code placed after the comment //Here will result in calling the getFields method resulting in the output of the string "Agg"?
1) System.out.println(a.getFields());
2) System.out.println(a.name);
3) System.out.println((Base) a.getFields());
4) System.out.println( ((Agg) a).getFields());
Q2.What will happen when you attempt to compile and run the following code?
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
inc.fermin(i);
i = i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
1) Compile time error
2) Output of 2
3) Output of 1
4) Output of 0
Q3.What will happen when you attempt to compile and run the following code?
class Base{
Base(){
System.out.println("Base");
}
}
public class Checket extends Base{
public static void main(String argv[]){
Checket c = new Checket();
super();
}
Checket(){
System.out.println("Checket");
}
}
1) Compile time error
2) Checket followed by Base
3) Base followed by Checket
4) runtime error
Q4.Which of the following statements are true?
1) A method cannot be overloaded to be less public in a child class
2) To be overridden a method only needs the same name and parameter types
3) To be overridden a method must have the same name, parameter and return types
4) An overridden method must have the same name, parameter names and parameter types
Q5.Given the following class definition
public class Upton{
public static void main(String argv[]){
}
public void amethod(int i){}
//Here
}
Which of the following would be legal to place after the comment //Here ?
1) public int amethod(int z){}
2) public int amethod(int i,int j){return 99;}
3) protected void amethod(long l){ }
4) private void anothermethod(){}
Q6.Which of the following most closely describes the process of overriding?
1) A class with the same name replaces the functionality of a class defined earlier in the hierarchy
2) A method with the same name completely replaces the functionality of a method earlier in the hierarchy
3) A method with the same name but different parameters gives multiple uses for the same method name
4) A class is prevented from accessing methods in its immediate ancestor
Q7.Which of the following statements are true?
1) The default constructor has a return type of void
2) The default constructor takes a parameter of void
3) The default constructor takes no parameters
4) The default constructor is not created if the class has any constructors of its own
Q8.What will happen when you attempt to compile and run the following class?
class Base{
Base(int i){
System.out.println("Base");
}
}
class Severn extends Base{
public static void main(String argv[]){
Severn s = new Severn();
}
void Severn(){
System.out.println("Severn");
}
}
1) Compilation and output of the string "Severn" at runtime
2) Compile time error
3) Compilation and no output at runtime
4) Compilation and output of the string "Base"
Q9.Which of the following statements are true?
1) Constructors cannot have a visibility modifier
2) Constructors can be marked public and protected, but not private
3) Constructors can only have a primitive return type
4) Constructors are not inherited
Q10.What will happen when you attempt to compile and run the following code
class Base{
public void Base(){
System.out.println("Base");
}
}
public class In extends Base{
public static void main(String argv[]){
In i=new In();
}
}
1) Compile time error Base is a keyword
2) Compilation and no output at runtime
3) Output of Base
4) Runtime error Base has no valid constructor
Q11.What will happen when you attempt to compile and run the following code
class Base{
private void amethod(int iBase){
System.out.println("Base.amethod");
}
}
class Over extends Base{
public static void main(String argv[]){
Over o = new Over();
int iBase=0;
o.amethod(iBase);
}
public void amethod(int iOver){
System.out.println("Over.amethod");
}
}
1) Compile time error complaining that Base.amethod is private
2) Runtime error complaining that Base.amethod is private
3) Output of "Base.amethod"
4) Output of "Over.amethod"
Q12.Which of the following will successfully create an instance of the Vector class and add an element?
1) Vector v=new Vector(99);
v[1]=99;
2) Vector v=new Vector();
v.addElement(99);
3) Vector v=new Vector();
v.add(99);
4 Vector v=new Vector(100);
v.addElement("99");
Q13.What will happen when you try compiling and running this code?
public class Ref{
public static void main(String argv[]){
Ref r = new Ref();
r.amethod(r);
}
public void amethod(Ref r){
int i=99;
multi(r);
System.out.println(i);
}
public void multi(Ref r){
r.i = r.i*2;
}
}
1) Error at compile time
2) An output of 99
3) An output of 198
4) An error at runtime
Q14.Given the following code how could you invoke the Base constructor that will print out the 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()
{
//Three
}
}
1) On the line After //One put Base(10);
2) On the line After //One put super(10);
3) On the line After //Two put super(10);
4) On the line After //Three put super(10);
Q15.Which of the following methods can be legally inserted in place of the comment //Method Here ?
class Base{
public void amethod(int i) { }
}
public class Scope extends Base{
public static void main(String argv[]){
}
//Method Here
}
1) void amethod(int i) throws Exception {}
2) void amethod(long i)throws Exception {}
3) void amethod(long i){}
4) public void amethod(int i) throws Exception {}
Q16.Which of the following methods are members of the Vector class and allow you to input a new element
1) addElement
2) insert
3) append
4) addItem
Q17.Given the following class definition which of the following can be legally placed after the comment line
//Here ?
class Base{
public Base(int i){}
}
public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(10);
}
MyOver(int i){
super(i);
}
MyOver(String s, int i){
this(i);
//Here
}
}
1)MyOver m = new MyOver();
2)super();
3)this("Hello",10);
4)Base b = new Base(10);
Q18.Given the following class definition, which of the following methods could be legally placed after the comment //Here
public class Rid{
public void amethod(int i, String s){}
//Here
}
1)public void amethod(String s, int i){}
2)public int amethod(int i, String s){}
3)public void amethod(int i, String mystring){}
4) public void Amethod(int i, String s) {}
Q19.What happens when you attempt to compile and run these two files in the same directory?
//File P1.java
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
}
}
//File P2.java
public class P2 extends P1{
public static void main(String argv[]){
P2 p2 = new P2();
p2.afancymethod();
}
}
1) Both compile and P2 outputs "What a fancy method" when run
2) Neither will compile
3) Both compile but P2 has an error at run time
4) P1 compiles cleanly but P2 has an error at compile time
Q20.Which of the following are methods of the Collection interface?
1) iterator
2) isEmpty
3) toArray
4) setText
Module 6 – Advanced Language Features
Q1.What will happen when you attempt to compile and run the following code.
public class Pvf{
static boolean Paddy;
public static void main(String argv[]){
System.out.println(Paddy);
}
}
1) Compile time error
2) compilation and output of false
3) compilation and output of true
4) compilation and output of null
Q2.What will happen when you attempt to compile and run the following code
class Base{
protected int i = 99;
}
public class Ab{
private int i=1;
public static void main(String argv[]){
Ab a = new Ab();
a.hallow();
}
abstract void hallow(){
System.out.println("Claines "+i);
}
}
1) Compile time error
2) Compilation and output of Claines 99
3) Compilation and output of Claines 1
4) Compilation and not output at runtime
Q3.Given the following code
class Base{
static int oak=99;
}
public class Doverdale extends Base{
public static void main(String argv[]){
Doverdale d = new Doverdale();
d.amethod();
}
public void amethod(){
//Here
}
}
Which of the following if placed after the comment //Here, will compile and modify the value of the variable oak?
1) super.oak=1;
2) oak=33;
3) Base.oak=22;
4) oak=50.1;
Q4.Which of the following statements are true?
1) A method in an interface must not have a body
2) A class may extend one other class plus at most one interface
3) A class may extends at most one other class plus implement many interfaces
4) An class accesses an interface via the keyword uses
Q5.Which of the following statements are true?
1) static methods do not have access to the implicit variable called this
2) A static method may be called without creating an instance of its class
3) A static method may not be overriden to be non-static
4) A static method may not be overloaded
Q6.Given the folowing classes which of the following will compile without error?
interface IFace{}
class CFace implements IFace{}
class Base{}
public class ObRef extends Base{
public static void main(String argv[]){
ObRef ob = new ObRef();
Base b = new Base();
Object o1 = new Object();
IFace o2 = new CFace();
}
}
1)o1=o2;
2)b=ob;
3)ob=b;
4)o1=b;
Q7.Given the following code
class Base{}
public class MyCast extends Base{
static boolean b1=false;
static int i = -1;
static double d = 10.1;
public static void main(String argv[]){
MyCast m = new MyCast();
Base b = new Base();
//Here
}
}
Which of the following, if inserted at the comment //Here will allow the code to compile and run without error
1) b=m;
2) m=b;
3) d =i;
4) b1 =i;
Q8.What will happen when you attempt to compile and run the following code with the command line "hello there"
public class Arg{
String[] MyArg;
public static void main(String argv[]){
MyArg=argv;
}
public void amethod(){
System.out.println(argv[1]);
}
}
1) Compile time error
2) Compilation and output of "hello"
3) Compilation and output of "there"
4) None of the above
Q9.What will happen when you attempt to compile and run this code?
class Base{
abstract public void myfunc();
public void another(){
System.out.println("Another method");
}
}
public class Abs extends Base{
public static void main(String argv[]){
Abs a = new Abs();
a.amethod();
}
public void myfunc(){
System.out.println("My func");
}
public void amethod(){
myfunc();
}
}
1) The code will compile and run, printing out the words "My Func"
2) The compiler will complain that the Base class is not declared as abstract.
3) The code will compile but complain at run time that the Base class has non abstract methods
4) The compiler will complain that the method myfunc in the base class has no body, nobody at all to looove it
Q10.What will happen when you attempt to compile and run this code?
abstract class Base{
abstract public void myfunc();
public void another(){
System.out.println("Another method");
}
}
public class Abs extends Base{
public static void main(String argv[]){
Abs a = new Abs();
a.amethod();
}
public void myfunc(){
System.out.println("My Func");
}
public void amethod(){
myfunc();
}
}
1) The code will compile and run, printing out the words "My Func"
2) The compiler will complain that the Base class has non abstract methods
3) The code will compile but complain at run time that the Base class has non abstract methods
4) The compiler will complain that the method myfunc in the base class has no body, nobody at all to looove it
Q1.Given the following code
import java.io.*;
public class Ppvg{
public static void main(String argv[]){
Ppvg p = new Ppvg();
p.fliton();
}
public int fliton(){
try{
FileInputStream din = new FileInputStream("Ppvg.java");
din.read();
}catch(IOException ioe){
System.out.println("flytwick");
return 99;
}finally{
System.out.println("fliton");
}
return -1;
}
}
Assuming the file Ppvg.java is available to be read which of the following statements are true if you try to compile and run the program?
1) The program will run and output only "flytwick"
2) The program will run and output only "fliton"
3) The program will run and output both "fliton" and "flytwick"
4) An error will occur at compile time because the method fliton attempts to return two values
Q2.What will happen when you attempt to compile and run the following code
import java.io.*;
class Base{
public void amethod()throws FileNotFoundException{}
}
public class ExcepDemo extends Base{
public static void main(String argv[]){
ExcepDemo e = new ExcepDemo();
}
public void amethod(){}
protected ExcepDemo(){
try{
DataInputStream din = new DataInputStream(System.in);
System.out.println("Pausing");
din.readByte();
System.out.println("Continuing");
this.amethod();
}catch(IOException ioe) {}
}
}
1) Compile time error caused by protected constructor
2) Compile time error caused by amethod not declaring Exception
3) Runtime error caused by amethod not declaring Exception
4) Compile and run with output of "Pausing" and "Continuing" after a key is hit
Q3.You want to find out the value of the last element of an array. You write the following code. What will happen when you compile and run it.?
public class MyAr{
public static void main(String argv[]){
int[] i = new int[5];
System.out.println(i[5]);
}
}
1) An error at compile time
2) An error at run time
3) The value 0 will be output
4) The string "null" will be output
Q4.Given the following code
import java.io.*;
public class Th{
public static void main(String argv[]){
Th t = new Th();
t.amethod();
}
public void amethod(){
try{
ioCall();
}catch(IOException ioe){}
}
}
What code would be most likely for the body of the ioCall method
1) public void ioCall ()throws IOException{
DataInputStream din = new DataInputStream(System.in);
din.readChar();
}
2) public void ioCall ()throw IOException{
DataInputStream din = new DataInputStream(System.in);
din.readChar();
}
3) public void ioCall (){
DataInputStream din = new DataInputStream(System.in);
din.readChar();
}
4) public void ioCall throws IOException(){
DataInputStream din = new DataInputStream(System.in);
din.readChar();
}
Q5.What will be output if you try to compile and run the following code, but 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
Module 11 – Java Foundation Classes
Q1.You are creating an application that has a form with a text entry field used to enter a persons age. Which of the following is appropriate for capturing this information.
1) Use the Text field of a TextField and parse the result using Integer
2) Use the getInteger method of the TextField
3) Use the getText method of a TextBox and parse the result using the getInt method of Integer class
4) Use the getText method of a TextField and use the parseInt method of the Integer class
Q2.Which of the following statements are true?
1) The default layout manager for an Applet is FlowLayout
2) The default layout manager for a Frame is FlowLayout
3) A layout manager must be assigned to an Applet before the setSize method is called
4) The FlowLayout manager attempts to honor the preferred size of any components
Q3.Which of the following are true?
1) A component may have only one event listener attached at a time
2) An event listener may be removed from a component
3) The ActionListener interface has no corresponding Adapter class
4) The processing of an event listener requires a try/catch block
Q4.What will happen when you attempt to compile and run this code
//Demonstration of event handling
import java.awt.*;
import java.awt.event.*;
public class MyWc extends Frame implements WindowListener{
public static void main(String argv[]){
MyWc mwc = new MyWc();
}
public void windowClosing(WindowEvent we){
System.exit(0);
}//End of windowClosing
public void MyWc(){
setSize(300,300);
setVisible(true);
}
}//End of class
1) Error at compile time
2) Visible Frame created that that can be closed
3) Compilation but no output at run time
4) Error at compile time because of comment before import statements
Q5.What most closely matches the appearance when this code runs?
import java.awt.*;
public class CompLay extends Frame{
public static void main(String argv[]){
CompLay cl = new CompLay();
}
CompLay(){
Panel p = new Panel();
p.setBackground(Color.pink);
p.add(new Button("One"));
p.add(new Button("Two"));
p.add(new Button("Three"));
add("South",p);
setLayout(new FlowLayout());
setSize(300,300);
setVisible(true);
}
}
1) The buttons will run from left to right along the bottom of the Frame
2) The buttons will run from left to right along the top of the frame
3) The buttons will not be displayed
4) Only button three will show occupying all of the frame
Q6.How do you change the current layout manager for a container
1) Use the setLayout method
2) Once created you cannot change the current layout manager of a component
3) Use the setLayoutManager method
4) Use the updateLayout method
Q7.How do you indicate where a component will be positioned using Flowlayout?
1) North, South,East,West
2) Assign a row/column grid reference
3) Pass a X/Y percentage parameter to the add method
4) Do nothing, the FlowLayout will position the component
Q8.Given the following 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);
}
}
How could you set the frame surface color to pink
1)s.setBackground(Color.pink);
2)s.setColor(PINK);
3)s.Background(pink);
4)s.color=Color.pink
Q9.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
Q10.An Applet has its Layout Manager set to the default of FlowLayout. What code would be correct to change to another Layout Manager.
1) setLayoutManager(new GridLayout());
2) setLayout(new GridLayout(2,2));
3) setGridLayout(2,2);
4) setBorderLayout();
————————————————
版权声明:本文为CSDN博主「romans1981」的原创文章,遵循undefined版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/romans1981/article/details/327543
以上是关于java试题的主要内容,如果未能解决你的问题,请参考以下文章