单选题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"

题目
单选题
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?()
A

 line 1

B

 line 2

C

 line 5

D

 line 8


相似考题
更多“单选题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");   ”相关问题
  • 第1题:

    阅读下面程序 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


    正确答案:C
    解析:String类的concat方法原型为public String concat(String str),其功能是将指定字符串连到此字符串的末尾。如果参数字符串的长度为0,则返回此String对象。否则,创建一个新的String对象,用来表示由此String对象表示的字符序列和由参数字符串表示的字符序列串联而成的字符序列。所以本题中的结果为str1和str2串联而成的字符序列,即"abcABC"。

  • 第2题:

    C#中,string str = null 与 string str =””,请尽量用文字说明区别。(要点:说明详细的内存

    空间分配)


    正确答案:
    string str =”” 分配空间 

  • 第3题:

    下面的程序各自独立,请问执行下面的四个TestMemory 函数各有什么样的结果?

    ①void GetMemory(char * p)

    {

    p = (char * )malloc(100);

    }

    void TestMemory (void)

    {

    char *str = NULL;

    GetMemory (str);

    strcpy(str, "hello world");

    prinff(str);

    }

    ② char * GetMemory (void)

    {

    char p[ ] = "hello world";

    return p;

    }

    void TestMemory (void)

    {

    char * str = NULL;

    str = GetMemory( );

    printf(str);

    }

    ③void GetMemory(char * * p, int num)

    {

    * p = (char * )malloc(num);

    }

    void TestMemory (void)

    {

    char * str = NULL;

    GetMemory(&str, 100);

    strcpy( str, "hello" );

    printf(sir);

    }

    ④void TestMemory (void)

    {

    char *str = (char * )malloe(100);

    strepy (str, "hello" );

    free ( str );

    if(str ! = NULL)

    {

    strepy( str, "world" );

    printf(str);

    }

    }


    正确答案:程序1程序崩溃。因为GetMemory并不能传递动态内存TestMemory函数中的str一直都是 NULL。strcpy(str “hello world”);将使程序崩溃。 程序2可能是乱码。因为GetMemory返回的是指向“栈内存”的指针该指针的地址不是 NULL但其原来的内容已经被清除新内容不可知。 程序3能够输出hello但是会发生内存泄漏。 程序4篡改动态内存区的内容后果难以预料非常危险。因为free(str);之后str成为野指针if(str!=NULL)语句不起作用。
    程序1程序崩溃。因为GetMemory并不能传递动态内存,TestMemory函数中的str一直都是 NULL。strcpy(str, “hello world”);将使程序崩溃。 程序2可能是乱码。因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原来的内容已经被清除,新内容不可知。 程序3能够输出hello,但是会发生内存泄漏。 程序4篡改动态内存区的内容,后果难以预料,非常危险。因为free(str);之后,str成为野指针,if(str!=NULL)语句不起作用。

  • 第4题:

    下面是一段javabean程序,该程序的运行结果是( )。public class NullTest{public static void main(String[]?args){int M=0;String str=null;StringBuffer sb=new StringBuffer("=");sb.append(str);sb.append(M++);System.out.println(sb.toString( ));}}

    A.=null
    B.=null0
    C.=null1
    D.=nullM

    答案:B
    解析:
    本题考查学生对javabean程序的熟悉程度,尤其是数值类型数据和字符串类型数据的掌握情况。M是整型变量,其值为0,str是字符串,sb是字符串空间,其中存放字符“=”,append是字符串添加函数,M++为自增运算符,它的特点是先取M的值作为表达式的值,再进行自增运算。程序的运算过程是:先将null拼接到“=”的后面,得字符串“=null”,再将0作为字符拼接到“=null”的后面,得“=null0”,M自增为1,输出结果为:=null0。

  • 第5题:

    public class Test {  public static void main(String[] args) {  String str = NULL;  System.out.println(str);  }  }   What is the result?()  

    • A、 NULL
    • B、 Compilation fails.
    • C、 The code runs with no output.
    • D、 An exception is thrown at runtime.

    正确答案:B

  • 第6题:

    String str; System.out.println(str.length()); 以上语句运行的结果是显示0 。


    正确答案:错误

  • 第7题:

    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?()    

    • A、 line 1
    • B、 line 2
    • C、 line 5
    • D、 line 8

    正确答案:C

  • 第8题:

    public static void test(String str) { int check = 4;  if (check = str.length()) {  System.out.print(str.charAt(check -= 1) +“, “);  } else {  System.out.print(str.charAt(0) + “, “);  }  }  and the invocation:  test(”four”);  test(”tee”); test(”to”);  What is the result?() 

    • A、 r, t, t,
    • B、 r, e, o,
    • C、 Compilation fails.
    • D、 An exception is thrown at runtime.

    正确答案:C

  • 第9题:

    判断题
    String str="abcedf"; int length=str.length。
    A

    B


    正确答案:
    解析: 暂无解析

  • 第10题:

    单选题
    11. public static void test(String str) {  12. if(str == null | str.lellgth() == 0) {  13. System.out.println(”String is empty”);  14. } else {  15. System.out.println(”String is not empty”);  16. }  17. }  And the invocation:  31. test(llull);  What is the result?()
    A

     Au exception is thrown at runtime.

    B

     “String is empty” is printed to output.

    C

     Compilation fails because of au error in line 12.

    D

     “String is not empty” is printed to output.


    正确答案: A
    解析: 暂无解析

  • 第11题:

    单选题
    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?()
    A

     line 1

    B

     line 2

    C

     line 5

    D

     line 8


    正确答案: C
    解析: 此题需要将代码仔细看清楚,查询没有逻辑错误,if …else的使用没有问题,也没有拼写错误,错误在于第5行的“与”操作符的使用,逻辑操作符(logical operator)的“与” 应该是&&,而&是位逻辑操作符(bitwise logical operator)的“与”,使用的对象不一样,逻辑操作符的“与”的左右操作数都应该是布尔型(logical boolan)的值,而位逻辑操作符的左右操作数都是整型(integral)值。

  • 第12题:

    单选题
    public class Test {  public static void main(String[] args) {  String str = NULL;  System.out.println(str);  }  }   What is the result?()
    A

     NULL

    B

     Compilation fails.

    C

     The code runs with no output.

    D

     An exception is thrown at runtime.


    正确答案: C
    解析: NULL should be "null".

  • 第13题:

    阅读下面程序 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


    正确答案:C
    解析:本题考查字符串的使用。String类提供concat(str)方法,该方法将当前字符串对象与指定str字符串相连。题目程序中生成两个字符串变量str1和str2,并为其赋值,然后生成一个字符串变量str3,该字符串变量的值为表达式str1.concat(str2)的结果。表达式str1.concat(str3)是把字符串str1与字符串str2相连,结果为“abcABC”。
    因此,程序的运行结果是“abcABC”。本题的正确答案是选项C。

  • 第14题:

    阅读以下说明,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();}

    }


    正确答案:(1)Head=new Node(strHead); (2)tempnode=tempnode.getNext(); (3)tempnode.setNext(new Node(strtempnode.getNext())); (4)Head=Head.getNext(); (5)insert(str);
    (1)Head=new Node(str,Head); (2)tempnode=tempnode.getNext(); (3)tempnode.setNext(new Node(str,tempnode.getNext())); (4)Head=Head.getNext(); (5)insert(str); 解析:本题考查链表和栈的基本特征在Java中的实现。
    在对链表进行表头插入时,首先要判断该链表是否为空,如果为空,直接插入结点;如果非空,在插入结点时把该结点的指针域改成能指向下一个结点的地址。在队尾插入时,同样要判断该链表是否为空,如果为空,直接插入结点;如果非空,在插入结点时把上一个结点的指针域改成能指向该结点的地址。
    下面来具体分析代码,首先定义了一个结点类,类中有两个不同的构造函数和三个函数,分别用于获取结点数据域,设置下一个结点值和返回下一个结点值。第(1)空是函数insert()里面的代码,函数要实现的功能是将数据str的结点插入在整个链表前面。结合整个函数看,此空处要实现的功能是在非空链表的前面插入结点,需要指针域来存放下一个结点的地址,而下一个结点的地址就是Head,因此,此处应该填Head=new Node(str,Head)。
    第(2)空和第(3)空一起考虑,它们都是函数append()里面的内容。函数要实现的功能是将数据str的结点插入在整个链表尾部。这两空要实现的功能是在非空链表的尾部插入结点。这需要调用返回下一个结点值函数和设置下一个结点值函数,因此,第 (2)空和第(3)空的答案分别为tempnode=tempnode.getNext()和tempnode.setNext(new Node(str,tempnode.getNext()))。
    第(4)空是函数get()里面的内容,此函数的功能是移出链表第一个结点,并返回该结点的数据域,从整个函数来看,此空处的功能是让链表的地址Head指向下一个结点。因此,答案为Head=Head.getNext()。
    第(5)空就比较简单了,要实现的功能就是让数据进栈,而进栈操作是在栈顶进行插入的,因此,只要调用函数insert()即可,其参数是str,此空答案为insert(str)。

  • 第15题:

    在C#中,string str = null 与 string str = “” 请尽量使用文字或图象说明其中的区别。


    正确答案:
    答:string str = null 是不给他分配内存空间,而string str =  给它分配长度为空字符串的内存空间。

  • 第16题:

    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、 Compilation fails.
    • E、 An exception is thrown at runtime.

    正确答案:D

  • 第17题:

    String str="abcedf"; int length=str.length。


    正确答案:错误

  • 第18题:

    顺序执行下列程序语句后,则b的值是() String str = "Hello"; String b = str.substring(0,2);

    • A、Hello
    • B、hello
    • C、He
    • D、null

    正确答案:C

  • 第19题:

    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.

    正确答案:D

  • 第20题:

    11. public static void test(String str) {  12. if(str == null | str.lellgth() == 0) {  13. System.out.println(”String is empty”);  14. } else {  15. System.out.println(”String is not empty”);  16. }  17. }  And the invocation:  31. test(llull);  What is the result?() 

    • A、 Au exception is thrown at runtime.
    • B、 “String is empty” is printed to output.
    • C、 Compilation fails because of au error in line 12.
    • D、 “String is not empty” is printed to output.

    正确答案:A

  • 第21题:

    单选题
    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.


    正确答案: D
    解析: 暂无解析

  • 第22题:

    单选题
    public static void test(String str) { int check = 4;  if (check = str.length()) {  System.out.print(str.charAt(check -= 1) +“, “);  } else {  System.out.print(str.charAt(0) + “, “);  }  }  and the invocation:  test(”four”);  test(”tee”); test(”to”);  What is the result?()
    A

     r, t, t,

    B

     r, e, o,

    C

     Compilation fails.

    D

     An exception is thrown at runtime.


    正确答案: C
    解析: 暂无解析

  • 第23题:

    单选题
    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

     Compilation fails.

    E

     An exception is thrown at runtime.


    正确答案: C
    解析: 暂无解析

  • 第24题:

    判断题
    String str; System.out.println(str.length()); 以上语句运行的结果是显示0 。
    A

    B


    正确答案:
    解析: 暂无解析