[Java] Practice question library program reading questions

3. Write the result of running the following program

1、  import java.io.*;

public class abc

{   

public static void main(String args [ ])

{    

AB s = new AB("Hello!","I love JAVA.");

         System.out.println(s.toString( ));

    }

}

class AB {

  String s1;

  String s2;

  public AB(String str1, String str2)

  { 

s1 = str1;  

s2 = str2; 

}

  public String toString( )

  { 

return s1+s2;

}

}

Running result: Hello! I love JAVA.

2、  import java.io.* ;

    public class abc

    {

          public static void main(String args[ ])

          {    int i, s = 0 ;

               int a[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 };

               for ( i = 0 ; i < a.length ; i ++ )

                     if ( a[i]%3 = = 0 )  s += a[i] ; 

               System.out.println("s="+s);

           }

     }

Running result: s = 180

3、 import java.io.* ;

   public class abc

   {

         public static void main(String args[ ])

         { 

System.out.println("a="+a+"\nb="+b);  

}

    }

    class SubClass extends SuperClass

    {  int c;

       SubClass(int aa, int bb, int cc)

       {   

super(aa, bb);

           c=cc;

       }

    }

   class SubSubClass extends SubClass

   {   int a;

       SubSubClass(int aa, int bb, int cc)

       {   super(aa, bb, cc);

           A = aa+bb+cc;

        }

       void show()

       {  

System.out.println("a="+a+"\nb="+b+"\nc="+c);  

}

  }

Running result: a=60

       b=20

       c=30

three. program reading questions

1. The output of the following programs is equal .           

class StringTest1

{

public static void main(String[] args) 

{

String s1="hello";

String s2=new String("hello");

if(s1.equals(s2)){

System.out.println("equal");

}else{

System.out.println("not equal");

}

}

}

2. The output of the following program segment is 5 6 7 8 9 .           

public class TestArray

{

    public static void main(String  args[ ]){  

     int  i , j ;

int  a[ ] = { 5,9,6,8,7};

    for  ( i = 0 ; i < a.length-1; i ++ ) {

    int  k = i;

    for  ( j = i ; j < a.length ;  j++ )

    if  ( a[j]<a[k] )  k = j;

   int  temp =a[i];

    a[i] = a[k];

    a[k] = temp;

    }

    for  ( i =0 ; i<a.length; i++ )

   System.out.print(a[i]+"  ");

       System.out.println( );

   }

}

3. Write the function of the following program.

import java.io.*;

public class TestFile

{

   public static void main(String args[]) throws Exception

    {

    BufferedReader br = new BufferedReader(

new InputStreamReader(System.in));

    BufferedWriter bw = new BufferedWriter(new FileWriter(“input.txt"));

    String s;

    while (true)

 {

      System.out.print("Please enter a string: ");

      System.out.flush();

      s=br.readLine();

      if (s.length()==0) break;

      bw.write(s);

      bw.newLine();

    }

    bw.close();

  }

}

Function: Input character strings one by one from the keyboard, and output them to the input.txt file one by one until a blank line is entered.

4. Read the following program and write the output.

class  Animal {

  Animal() {

    System.out.print ("Animal  ");  }

}

public  

class  Dog  extends  Animal {

 Dog() {

    System.out.print ("Dog ");  }

  public static void main(String[] args) {

    Dog  snoppy= new  Dog();  }

}

Output result: Animal Dog

3. Program reading questions

1. The output of the following program is _  Peter is 17 years old! ___.

public class Person {

String name;

int  age;

public Person(String name, int age) {

this.name = name;

this.age = age;

}

public static void main(String[] args) {

Person c = new Person("Peter", 17);

System.out.println(c.name + " is " + c.age + " years old!");

}

}

2. The output of the following program is __ course number: 101 course name: ASP credits: 3 ___ .

public class Course {

private String cNumber;

private String cName;

private int cUnit;

public Course(String number, String name, int unit) {

cNumber = number;

cName = name;

cUnit  = united;

}

public void printCourseInfo() {

System.out .println ( "Course Number:"  +  cNumber  +  "Course Name:"  +  cName  +  "Credit:"  +  cUnit );

}

}

class CourseTest {

public static void main(String[] args) {

Course c;

c = new Course("101""ASP", 3);

c.printCourseInfo();

}

}

3. The output of the following program is __Tom cat weight : 20.0 kg____ .

public class Tom {

private float weight;

private static String name;

public void setWeight(float weight) {

this.weight = weight;

}

private void out() {

System.out .println ( name  +  "weight:"  +  weight  +  "jin" );

}

public static void main(String[] args) {

Tom.name  =  "Tom the cat " ;

Tom cat = new Tom();

cat.setWeight(20);

cat.out();

}

}

4. The output of the following program_Name : Tom Age: 15 Home Address: Jinshui District Tel: 66123456 School: No. 9 Middle School_ .

public class Father {

String nameaddresstel;

int  age;

public Father(String name, int age) {

this.name = name;

this.age = age;

}

void out() {

System.out.print ( "Name:" + name  )  ;

System.out.print ( "age:" +  age  ) ;

}

void outOther() {

System.out.print ( "home address:" + address  )  ;

System.out.print ( "Tel:" +  tel  ) ;

}

}

class Son extends Father {

String school;

public Son(String name, int age) {

super(name, age);

}

void out() {

super.out();

super.outOther();

System.out .println ( " School: "  +  school );

}

public static void main(String args[]) {

Son son = new Son("Tom", 15);

son.address  =  " Golden Water District" ;

son.school  =  "Nine Middle School " ;

son.tel = "66123456";

son.out();

}

}

5. The running result of the following program is __ 12345 ____.

public class MyClass {

int  a[] = { 1, 2, 3, 4, 5 };

void out() {

for (int j = 0; j < a.length; j++)

System.out.print(a[j] + "");

}

public static void main(String[] args) {

MyClass my = new MyClass();

my.out();

}

}

3. Program reading questions

1. Read the following program and answer the questions (3 points for u, 3 points for v, 6 points in total) .

 import java.awt.*;

    import javax.swing.*;

    public class T extends JFrame {

        public T ( ) {

            super("GridLayout");

            Container  con=this.getContentPane();

            con.setLayout(new GridLayout(2,3));

            con.add(new JButton("a"));  

            con.add(new JButton("b"));

            con.add(new JButton("c"));  

            con.add(new JButton("d"));

            con.add(new JButton("e"));

            con.add(new JButton("f"));

            setSize(200, 80);

            setVisible(true);

        }

        public static void main(String args[]) {

           new T();        

        }

}

u draws a picture to represent the graphical interface after the program runs.

v If the program handles the action event of the button by implementing an interface, what is the name of the interface? What about method header declarations in interfaces?

Answer:

u

vInterface name: ActionListener

  The method in the interface: public void actionPerformed(ActionEvent e)

2. Read the following program and answer the questions (3 points for u, 3 points for v, 6 points in total) . .

import  java.util.*;

public class T   {    

        public static void main(String args[])  {

            Set set = new TreeSet();

            set.add(new Integer(10));        

            set.add(new Integer(5));   

            set.add(new Integer(15));

            set.add(new Integer(5));   

            set.add(new Integer(10));

            System.out.println("size = " + set.size());

            Iterator   it=set.iterator();

            while(it.hasNext()){

                System.out.print(it.next()+"   ");

            }

        }    

}

What is the output of the u program after running?

v Explain the main difference between collections (Set interface) and mapping (Map interface) in java.

Answer:

u

size = 3

5   10   15

The vSet interface is a collection that does not contain repeated elements; the Map interface maps keys to values, and keys can be repeated, but each key can map at most one value.

3. Read the following program and answer the questions (3 points for u, 3 points for v, 6 points in total) .

import java.io.*;

public class Test {

    public static void main(String args[]) throws IOException {

        BufferedReader buf=new BufferedReader(

                    new InputStreamReader(System.in));        

        while(true) {

            String  str=buf.readLine();

            if(str.equals("quit"))

                break;

            int x=Integer.parseInt(str);

            System.out.println(x*x);

        }

    }  

}

Compile and run the above program:

u input 5 from the keyboard, what is the output result after pressing Enter?

v Enter quit from the keyboard, and what is the execution of the program after pressing Enter?

Answer:

u25 

v Terminates the running of the application.

3. Program reading questions

1. Read the program code below and answer the questions (3 points for u, 3 points for v, 6 points in total) .

String s1 = new String("abcde");

String s2 = new String("abcde"); 

boolean b1= s1.equals(s2);

boolean b2 = s1== s2;            

System.out.print(b1+"   "+b2);           

What is the output of the command line after the u program segment is executed?

v Explain the reason for the result of output (1)?

Answer:

u true  false

The vequals method compares whether the contents of two strings are equal; the operator "==" judges whether two objects point to the same reference, that is, whether they are the same object.

2. Read the following program and answer the questions (3 points for u, 3 points for v, 6 points in total) .

import java.io.*;

public class Test {

    public static void main(String args[]) throws IOException {

        BufferedReader buf=new BufferedReader(

                    new InputStreamReader(System.in));        

        while(true) {

            String str = buf.readLine();

            if(str.equals("quit"))

                break;

            int x=Integer.parseInt(str);

            System.out.println(x*x);

        }

    }  

}

Compile and run the above program:

u input 10 from the keyboard, what is the output result after pressing Enter?

v Enter exit from the keyboard, can the program be executed correctly after pressing Enter? Why?

Answer:

u100  

v cannot; because the method Integer.parseInt(str) cannot convert the string "exit" into an integer, an exception is thrown.

3. Write the result after compiling and running the following program (6 points).

public class Test{

     public static void main(String args[]) {

         new Student("Tom", 'm', 90, 88);

         new Student("Jack", 'm', 66, 89);

         new Student("Mary", 'f', 76, 86);

         System.out.println("name\tsex\tchinese\tenglish");

         Student.print();

     }

 }

 

 class Student {

     protected  String   name;

     protected  char     sex;

     protected  int      chinese;

     protected  int      english;

     protected  Student  next;

     static Student list;

     

     Student (String name, char sex, int chinese, int english)   {

         this.name=name;

         this.sex=sex;

         this.chinese=chinese;

         this.english=english;

         this.next=list;

         list=this;

     }

     static void print() {

         Student friend=list;

         if (friend==null)

             System.out.println("The list is empty.");

         else {

             do{

                 System.out.println(friend.toString());

                 friend=friend.next;

             }while(friend!=null);

         }

     }

     public String toString()  {

         return new String(name+"\t"+sex+"\t"+chinese+"\t"+english);

     }

 }

Answer:

name    sex     chinese  english

Mary    f       76      86

Jack     m      66      89

Tom     m      90      88

3. Program fill-in-the-blank questions

1.public class Sum{

public static void main(String [] args){

int j=10;

System.out.println("j is : "+j);

calculate(j);

System.out.println("At last, j is : "+j);

}

static void calculate (int j){

for (int i = 0;i<10;i++)

j++;

System.out.println("j in calculate() is: "+j);

}

}

The output is:

j is :                 (1) 

j in calculate() is :     (2)

At last j is :           (3)

Answer: (1) 10; (2) 20; (3) 10.

2. Fill  in the blanks as required

abstract class SuperAbstract{

void a(){…}

abstract void b();

abstract int c(int i);

}

interface AsSuper

{

void x();

}

abstract class SubAbstract extends SuperAbstract implements AsSuper

{

public void b(){…}

abstract String f();

}

public class InheritAbstract extends SubAbstract{

public void x(){…}

public int c(int i ) {…}

public String f(){…}

public static void main(String args[]){

InheritAbstract instance=new InheritAbstract();

instance.x();

instance.a();

instance.b();

instance.c(100);

System.out.println(instance.f());

}

In the above program:

Abstract classes are: SuperAbstract and (1) (write the class name)

Non-abstract classes are: (2) (write the class name)

The interfaces are: (3) (write out the interface name)

The x() method in AsSuper is the (4) method, so it must be (5) in InheritAbstract       

Answer:

(1) SuperAbstract;

(2) InheritAbstract;

(3) AsSuper;

(4) Abstraction;

(5) Coverage and realization.

3. Follow  the notes to complete the program

public class Leaf {

private int i = 0; //This attribute value is used for inspection

Leaf increment(){ //Define the method increment(), the return value is an object of the Leaf class

i++;

return  (1)  ;//Return the address of the current object as the return value

}

void print() {

System.out.println(" i = " + i);

}

public static void main(String args[]){

Leaf x =   (2) ; //Create an object x of the Leaf class

x.increment().increment().increment().print();

}//The method increment() is called multiple times, and the address of x is returned, and the value of i indicates the number of calls

}

The output is i =   (3)      

Answer:

  1. this;
  2. new Leaf();
  3. 3

4.   Follow the notes to complete the file copy procedure

 //FileStream source code is as follows:

 import java.io.*;

 class FileStream {

   public static void main(String args []) { 

     try {

          File inFile = new File("file1.txt"); //Specify the source file

          File outFile = new File("file2.txt"); //Specify the target file

          FileInputStream fis =(1);

          FileOutputStream fos = new FileOutputStream(outFile);

       int c; 

          //Input from the source file byte by byte, and then output to the fos stream

while ((c = fis.read ())!=-1)

     (2);

          fis.close(); 

        fos.close();

}

catch (Exception e) {

       System.out.println("FileStreamsTest: "+e);

}

     }

}

Answer:

(1) new FileInputStream(inFile);

(2) fos.write(c);

5.   Read the program and give the result:

// The source code of AbstractClassDemo.java is as follows:

abstract class Shape { //Define the abstract class Shape and the abstract method display

abstract void display();

}

class Circle extends Shape {

void display() { //method to implement abstract class

System.out.println("Circle");

}

}

class Rectangle extends Shape {

void display() { //method to implement abstract class

System.out.println("Rectangle");

}

}

class Triangle extends Shape {

void display() { //method to implement abstract class

System.out.println("Triangle");

}

}

public class AbstractClassDemo{

public static void main(String args[]){

(new Circle()).display(); //Define an unnamed object to call the corresponding display method

(new Rectangle()).display();

(new Triangle()).display();

}

}

The output is ?

答案:(1) Circle; (2) Rectangle; (3) Triangle。

two. Read program questions

1.  Read the following code and tell the function of this program. 

import java.io.*; 

public class Test{ 

public static void main( String [] argv) { 

try { 

BufferedReader is = 

new BufferedReader( new InputStreamReader(System.in)); 

String inputLine; 

While ((inputLine = is.readLine ())!= null) { 

System.out.println(inputLine); 

is.close(); 

}catch (IOException e) { 

System.out.println("IOException: " + e); 

Answer: Read keyboard input and display it on the screen until the keyboard input is empty.

2. Read  the following program and write the correct running result. 

class test { 

public static void main (String [] args ){ 

int x=9, y; 

if (x>=0) 

if (x>0)

y=1; 

else y=0; 

else y=-1; 

System.out.println(y); 

Answer: 1

3. Read  the program and write the correct operation result. 

public class Father{ 

int a=100; 

public void miner(){

 a--; 

public static void main(String[] args){ 

Father x = new Father(); 

Son y = new Son(); 

System.out.println(y.a); 

System.out.println( y.getA()); 

y.miner(); 

System.out.println(y.a); 

System.out.println(y.getA()); 

class Son extends Father{ 

int a = 0; 

public void plus(){

   a++; 

public int getA() { 

return super.a; 

Answer:

0
100
0
99

Guess you like

Origin blog.csdn.net/CE00001/article/details/130163336