比赛中的一些题目,记录一下
1.如下代码:
byte[] array1,array2[];
byte array3[][];
int[][] array4;
假设每个数组都已经被正确初始化了,下面哪个语句会产生编译错误?
A array2 = array1;
B array2 = array3;
C array2 = array4;
2.
import java.util.*;
public class StupidString
{
private String s;
public StupidString(String s){
this.s = s;
}
public static void main(String[] args)
{
HashSet<Object> hs = new HashSet<>();
StupidString ws1 = new StupidString("I'm stupid.");
String s1 = new String("I'm stupid.");
StupidString ws2 = new StupidString("I'm stupid.");
String s2 = new String("I'm stupid.");
StupidString ws3 = new StupidString("Yes, rather stupid.");
hs.add(ws1);hs.add(s1);hs.add(ws2);hs.add(s2);hs.add(ws3);
System.out.println(hs.size());
}
}
<span style="font-size:18px;">请问编译运行的结果是什么?</span>
3int i=100;
for(i=0;i<100;i--){
for(i=200;i>=100;i--){
while(i == 100){
i *= i;
}
}
}
System.out.println(i);
请问编译运行的结果是什么?4.
int a = 4,b = 6,c = 3;
String s = "abc";
System.out.println(a+b+s+c);
请问编译运行的结果是什么?
5.
class GoodJob extends Thread
{
int x = 0;
public class Runner implements Runnable
{
public void run(){
int current = 0;
for(int i=0;i<4;i++){
current = x;
System.out.print(current+",");
x = current+2;
}
}
}
public static void main(String[] args)
{
new GoodJob().run();
}
public void run(){
Runnable r1 = new Runner();
new Thread(r1).start();
new Thread(r1).start();
}
}
以下哪些选项是程序可能的输出?(多选)
A 0 0 2 2 4 4 6 6 8 8 10 10 12 12 14 14
B 0 0 2 4 6 2 4 6
C 0 2 0 2 4 6 4 6
6
public class Yikes
{
public static void go(Long n){
System.out.println("Long ");
}
public static void go(Short n){
System.out.println("Short ");
}
public static void go(int n){
System.out.println("int ");
}
public static void main(String[] args)
{
short y = 6;
long z = y;
go(y);
go(z);
}
}
请问编译运行结果是?
7.
int i = 100;
i = i++;
请问此时i是多少?
8.
补充以下的代码,使之一定输出“1-2-3-4-5-”
public class HowToSync
{
private int x;
public void puzzle(){
____________{//只能写一句代码
int current = x;
x = current+1;
System.out.println(x+"-");
}
}
public void go(){
for(int i=0;i<5;i++){
new Thread(){
public void run(){
___________;//只能写一句代码
}
}.start();
}
}
public static void main(String[] args)
{
new HowToSync().go();
}
}
第二处写puzzle(),第一处写synchronized(HowToSync.class)不能写成while(x<5)
//请问如下代码的输出是什么?
import java.rmi.UnexpectedException;
public class WanWanMeiXiangDao
{
public static void main(String[] args)
{
try(DaChuiCareer daChui = new DaChuiCareer()){
daChui.happyLife();
return;
}catch(Exception e){
System.out.println(e.getClass().getSimpleName());
return;
}finally{
System.out.println("story end");
return;
}
}
public static class DaChuiCareer implements AutoCloseable
{ /*静态内部类还有构造函数,这里不用感到奇怪,静态内部类只是说明它不包含外部类的引用而已*/
public DaChuiCareer(){
System.out.println("born");
}
public void happyLife() throws JoyfulException{
System.out.println("happy living");
throw new JoyfulException();
}
@Override
public void close() throws UnexpectedException{}
}
}
class JoyfulException extends Exception{}
public class Sudo implements Runnable
{
public static void main(String[] args)throws Exception
{
Thread t = new Thread(new Sudo());
t.start();
System.out.println("Started!");
t.join();
System.out.println("Complete");
}
public void run(){
for(int i=0;i<4;i++){
System.out.print(i);
}
}
}
//哪个选项是可能的输出结果?
A 编译出错
B 运行时抛出异常
C 代码正常执行并输出: StartedComplete
D 代码正常执行并输出: StartedComplete0123
E 代码正常执行并输出: Started0123Complete</span>
public class Weibo
{
private String content;
public String getContent(){
return content;
}
public void setContent(String d){
content = d;
}
public static void modifyCount(Weibo weibo,String content){
weibo = new Weibo();
weibo.setContent(content);
}
public static void main(String[] args)
{
Weibo wb = new Weibo();
wb.setContent("javacontest");
Weibo wb2 = new Weibo();
wb2.setContent("jingkao");
modifyCount(wb,"sudocn");
System.out.println(wb.getContent());
System.out.println(wb2.getContent());
}
}
请问编译运行的结果是什么?
public class Java
{
Integer i;
int x;
public Java(int y){
x = i+y;
System.out.println(x);
}
public static void main(String[] args)
{
new Java(new Integer(4));
}
}
请问编译运行的结果是什么?
A 输出4
B 编译失败,因为第5行出现错误
C 编译失败,因为第9行出现错误
D 在运行时出现一个IlegalStateException异常
E 在运行时出现一个NullPointerException异常
假设已经有了正确的import语句,下面代码
PriorityQueue<String> pq = new PriorityQueue<String>();
pq.add("2");
pq.add("4");
System.out.print(pq.peek()+" ");
pq.offer("1");
pq.add("3");
pq.remove("1");
System.out.print(pq.poll()+" ");
if(pq.remove("2")) System.out.print(pq.poll()+" ");
System.out.println(pq.poll()+" "+pq.peek());
编译运行的结果是什么?
A 2 2 3 3
B 2 2 3 4
C 4 3 3 4
package org.javacontest;
public class Browser
{
public Frame fr = new Frame();
class Frame
{
public int width;
public int height;
}
}
请问变量width可以被哪些类访问?
A 任何类都可以
B 任何类都不行
C 只能被org.javacontest包中的类访问
D 只能被Browser的子类访问
关于内部类访问权限倒是想多说几句,内部类的访问权限是由内部类修饰符和外部类的修饰符的组合决定的。因为访问内部类必须首先能访问外部类。比如说,外部类是default,内部类是protected,那么只有同一个包中的类可以访问该内部类。class OhMyException
{
static String s = ">";
public static void main(String[] args)
{
try{
throw new Exception();
}catch(Exception e){
try{
try{
throw new Exception();
}catch(Exception ex){ s+= "eric";}
throw new Exception();
}catch(Exception x){
s += "michael";
}finally{
s += "fyi";
}
}finally{
s += "chao";
}
System.out.println(s);
}
}
请问程序输出结果是什么?
对于java.io.BufferWriter和java.io.FileWriter这两个类,下面哪一个功能
只有其中的一个类直接提供了方法支持?
A 关闭流
B 冲刷流的缓冲
C 往流里写数据
D 在流里标记一个位置
E 在流里写入一个行分割符
class Fat
{
String name = "No name";
public Fat(String nm){
name = nm;
}
}
class Steven extends Fat
{
String weight = "80kg";
public Steven(String w){
weight = w;
}
}
public class FatTest
{
public static void main(String[] args){
Steven s = new Steven("90kg");
System.out.println(s.weight);
}
}
请问编译运行的结果是?
A 90kg
B 80kg
C 运行时抛出异常
D 编译失败
图片中答案是错误的