Given the following code fragment: 1) String str = null; 2) if ((str != null) && (str.length() > 10)) { 3) System.out.println("more than 10"); 4) } 5) else if ((str != null) & (str.length() < 5)) { 6) System.out.println("less than 5"); 7) } 8) else { System.out.println("end"); } Which line will cause error?()
第1题:
Giventhefollowingcodefragment:1)Stringstr=null;2)if((str!=null)&&(str.length()>10)){3)System.out.println("morethan10");4)}5)elseif((str!=null)&(str.length()<5)){6)System.out.println("lessthan5");7)}8)else{System.out.println("end");}Whichlinewillcauseerror?()
A.line1
B.line2
C.line5
D.line8
此题需要将代码仔细看清楚,查询没有逻辑错误,if…else的使用没有问题,也没有拼写错误,错误在于第5行的“与”操作符的使用,逻辑操作符(logicaloperator)的“与”应该是&&,而&是位逻辑操作符(bitwiselogicaloperator)的“与”,使用的对象不一样,逻辑操作符的“与”的左右操作数都应该是布尔型(logicalboolan)的值,而位逻辑操作符的左右操作数都是整型(integral)值。
第2题:
A.NULL
B.Compilationfails.
C.Thecoderunswithnooutput.
D.Anexceptionisthrownatruntime.
NULLshouldbe"null".
第3题:
public static void main(String[]args){String str="null";if(str==null){System.out.println("null");}else(str.length()==0){System.out.println("zero");}else{System.out.println("some");}}What is the result?()
A.null
B.zero
C.some
D.Compilationfails.
E.Anexceptionisthrownatruntime.
第4题:
C#中,string str = null 与 string str =””,请尽量用文字说明区别。(要点:说明详细的内存
空间分配)
第5题:
阅读下面程序 public class ConcatTest { public static void main(String[] args) { String str1="abc"; String str2="ABC": String str3=str1.concat(str2); System.out.println(str3); } } 程序运行的结果是
A.abc
B.ABC
C.abcABC
D.ABCabc
第6题:
Whatwillbetheresultofattemptingtorunthefollowingprogram?()publicclassQaa75{publicstaticvoidmain(Stringargs[]){String[][][]arr={{{},null},{{"1","2"},{"1",null,"3"}},{},{{"1",null}}};System.out.println(arr.length+arr[1][2].length);}}
A.TheprogramwillterminatewithanArrayIndexOutOfBoundsException.
B.TheprogramwillterminatewithaNullPointerException.
C.4willbewrittentostandardoutput.
D.6willbewrittentostandardoutput.
E.7willbewrittentostandardoutput.
第7题:
A、2个
B、3个
C、4个
D、5个
第8题:
A.null
B.zero
C.some
D.Compilationfails.
E.Anexceptionisthrownatruntime.
第9题:
阅读下面程序 public class ConcatTest{ public static void main(String[] args) { String strl = "abc"; String str2 = "ABC"; String str3 = str1.concat(str2); System.out.println(str3); } } 程序的运行结果是:
A.abe
B.ABC
C.abcABC
D.ABCabc
第10题:
阅读以下说明,Java代码将应填入(n)处的字句写在对应栏内。
【说明】
链表和栈对象的共同特征是:在数据上执行的操作与在每个对象中实体存储的基本类型无关。例如,一个栈存储实体后,只要保证最后存储的项最先用,最先存储的项最后用,则栈的操作可以从链表的操作中派生得到。程序6-1实现了链表的操作,程序6-2实现了栈操作。
import java.io.*;
class Node //定义结点
{ private String m_content;
private Node m_next;
Node(String str)
{ m_content=str;
m_next=null; }
Node(String str,Node next)
{ m_content=str;
m_next=next; }
String getData() //获取结点数据域
{ return m_content;}
void setNext(Node next] //设置下一个结点值
{ m_next=next; }
Node getNext() //返回下一个结点
{ return m_next; )
}
【程序6-1】
class List
{ Node Head;
List()
{ Head=null; }
void insert(String str) //将数据str的结点插入在整个链表前面
{ if(Head==null)
Head=new Node(str);
else
(1)
}
void append(String str) //将数据str的结点插入在整个链表尾部
{ Node tempnode=Head;
it(tempnode==null)
Heed=new Node(str);
else
{ white(tempnode.getNext()!=null)
(2)
(3) }
}
String get() //移出链表第一个结点,并返回该结点的数据域
{ Srting temp=new String();
if(Head==null)
{ System.out.println("Errow! from empty list!")
System.exit(0); }
else
{ temp=Head.getData();
(4) }
return temp;
}
}
【程序6-2】
class Stack extends List
{ void push(String str) //进栈
{ (5) }
String pop() //出栈
{ return get();}
}