SCJ147

1、非运算~

public class demo01 {
    public static void main(String[] args) {
        int i=0xFFFFFFF1;
        int j=~i;
        System.out.println(j);
    }
}
//结果为14  ~表示非运算符 即将i转化为二进制后按位取反

2、==与equals

public class demo02 {
    public static void main(String[] args) {
        Integer i = new Integer (42);
        Long l = new Long (42);
        Double d = new Double (42.0);
        System.out.println("i==42为"+(i==42));//i
        System.out.println("i.equals(l)为"+i.equals(l));
        System.out.println("i.equals(d)为"+i.equals(d));
        System.out.println("i.equals(42)为"+i.equals(42));
        //equals进行值的比较
    }
}

在这里插入图片描述 equal先比较是否为相同的类,再比较是否为相同的值

3、逻辑或与按位或

public class demo03 {
    private static int j = 0;
    private static boolean methodB(int k){
        j+=k;
        return true;
    }
    public static void methodA(int  i) {
        boolean b;
        b = i < 10 | methodB (4);//按位或 左右两边都一定执行
        b = i < 10 || methodB (8);//逻辑或  只要左边为true就不执行右边
    }
    public static void main(String[] args) {
        methodA(0);
        System.out.println(j);
    }
}
//运行结果 4  

4、异或运算符

public class demo04 {
    public static void main(String[] args) {
        System.out.println(6^3);//相同取反 相反取1
    }
}

5、传参

public class demo05 {
    public static void main(String[] args) {
        StringBuffer a=new StringBuffer("A");
        StringBuffer b=new StringBuffer("B");
        operate(a,b);
        System.out.println(a+","+b);
    }
    static void operate(StringBuffer x,StringBuffer y){
        x.append(y);
        y=x;//y指向了x,但b本身没有改变
    }
}

输出:ABB

6、String与StringBuffer

public class demo06 {
    public static void Stringreplace(String text){
        text =text.replace('j','i');
    }
    public static void Bufferreplce(StringBuffer text){
        text=text.append('c');
    }
    public static void main(String[] args) {
        String s1=new String("java");
        StringBuffer s2=new StringBuffer("java");
        Stringreplace(s1);
        Bufferreplce(s2);
        System.out.println(s1+s2);
    }
}
一旦一个String对象在内存中创建,它将是不可改变的。
所有的String类中方法并不是改变String对象自己,而是重新创建一个新的String对象。

7、Interger

public class demo07 {
    public static void add(Integer i){
        int val=i.intValue();
        val+=3;
        i=new Integer(val);

    }
    public static void main(String[] args) {
        Integer i=new Integer(0);
        add(i);
        System.out.println(i.intValue());
    }
}
Integer被final修饰,为不可变对象,一旦创建不可修改。
修改后的值为新创建的对象。

8、构造函数的重载

public class demo08 {
    public demo08(int x,int y,int z){};
    为上述语句的重载形式有:
    demo08(){};
    protected int demo08(){};
    private demo08 (int x,int y,byte z){};
    public Object demo08(int x,int y,int z){};
    public void demo08(byte x,byte y,byte z){};
}

demo08为构造函数,故其重载也应为构造函数,无返回值。即第1和第3个。

9、方法的重载

public class demo09 {
    public void setVar(int a,int b,float c){};
    //为上述语句的重载形式有:
    private void setVar(int a,float c,int b){};
    protected void setVar(int a,int b,float c){};
    public int setVar(int a,float c,int b){return a};
    public int setVar(int a,int b,float c){return a};
    protected float setVar(int a,int b,float c){return c};
}
判断重载的依据:形参的个数,类型,顺序
修饰词和返回值不能作为判断是否重载的依据

10、重写

class demo10 {
    private float x=1.0f;
    protected float getVar(){return x};
}
class demo10x extends demo10{
    private float x=2.0f;
    //哪两个可以作为getVar的重写
    float getVar(){return x};//defalut
    public float getVar(){return x};
    public double getVar(){return x};//返
    protected float getVar(){return x};
    public float getVar(float f){return f};
}
重写的原则:
①返回值、方法 名、参数列表完全一致
②子类异常不能大于父类异常
③子类访问级别不能低于父类访问级别(public>protected>无修饰词>priviate)
故选2和4

13、静态内部类

package foo;
public class demo13 {
    public static class Inner{};
}
Which statement is true?
A. An instance of the Inner class can be constructed with “new Outer.Inner ()”
B. An instance of the inner class cannot be constructed outside of package foo
C. An instance of the inner class can only be constructed from within the outer class
D. From within the package bar, an instance of the inner class can be constructed with “new inner()//为静态内部类,故可直接创建,无需通过外部类的实例对象创建 故选A
/*
静态内部类可以直接创建对象new B.C();
如果内部类不是静态的,那就得这样
B b = new B();
B.C c = b.new C(); 
*/

14、成员内部类

public class demo14 {//外部类
    public class insideone{};//成员内部类
}
 class inertest {
    public static void main(String[] args) {
        demo14 eo = new demo14();//创建外部类的实例对象
        demo14.insideone ei=eo.new insideone();//第4个为正确创建 
    }
}
 成员内部类的创建格式: 
 外部类.内部类 对象名=new 外部类().new 内部类实例对象();
 new 外部类名称()为匿名对象的创建,也可如上例拆成两步。

15、接口

public class demo15 implements Foo{
    public static void main(String[] args) {
        int i;
        demo15 test =new demo15();
        i=test.k;
        i=demo15.k;
        i=Foo.k;
        //如果加上Foo.k=15;会报错
    }
}

public interface Foo {
    int k=0;
}
编译通过。
接口成员变量特点:只能是常量。默认修饰符 public static final。
使用:常量被静态修饰,可以被类名直接调用,被接口的名字直接调用。

16、导包

import java.io.PrintWriter;

public class demo16 {
    public static void main(String[] args) throws Exception{
        PrintWriter out=new PrintWriter(new java.io.OutputStreamWriter(System.out),true);
        out.println("Hello");

    }

17、关键字

public class demo17 {
    Which two statements are reserved words in Java? (Choose Two)
    A.run
    B.import
    C.default
    D.implement
BC implemets为关键字,但implement不是

18、自动类型转换

18. Which three are valid declarations of a float? (Choose Three) 
A. float foo = -1;//整型 自动转换为float
B. float foo = 1.0;//默认小数为double,无法转为float
C. float foo = 42e1;//科学计数法,浮点指数,表示42乘以10的1次方;
D. float foo = 2.02f;//加上f代表为float型
E. float foo = 3.03d;//带d为double型
F. float foo = 0x0123;//16进制的123,为char?
/*
	java中浮点型默认为Double
	科学计数法形式。如:123e3或123E3,其中e或E之前必须有数字,且e或E后面的指数必须为整数。
	实常数在机器中占64位,具有double型的值。对于float型的值,则要在数字后加f或F,如12.3F,它在机器中占32位,且表示精度较低。
*/
ADF
自动类型转换必须满足转换前的数据类型的位数要低于转换后的数据类型。
例如: short数据类型的位数为16位,就可以自动转换位数为32的int类型
同样float数据类型的位数为32,可以自动转换为64位的double类型。
byte,short,char->int->long->float->double

19、boolean与Boolean

public class day19 {
    public static void main(String[] args) {
        int index=1;
        boolean[] test=new Boolean[3];//应该不是B的锅吧
        boolean foo=test[index];
    }
}
	What is the result?
	A. foo has the value of 0
	B. foo has the value of null
	C. foo has the value of true
	D. foo has the value of false
	E. an exception is thrown
	F. the code will not compile
	
如果为B则编译错误,如果为b则输出foo为flase。默认初始值
如果boolean型变量是类变量,则默认值为false.否则没有默认值。
如果是Boolean是类变量,则是包装类是对象,默认值是null,否则没有默认值。

20、String[ ] args

public class demo20 {
    public static void main(String[] args) {
        String foo=args[1];
        String foo=args[2];
        String foo=args[3];//重复定义???编译错误??
        //否则args[1]为green? args[2]为blue?
    }
}
//参数String[ ] args的作用就是可以在main方法运行前将参数传入main方法中。

21、模拟计算

public class demo21 {
    public static void main(String[] args) {
        int index=1;
        int []foo=new int[3];
        int bar=foo[index];//默认初始值为0
        int baz=bar+index;
        System.out.println(baz);//baz为1
    }
}

22、String的初始化

public class demo22 {
    public static void main(String[] args) {
        String s;
        System.out.println("s="+s);
    }
编译不通过。
String一旦创建不可修改,故一定要进行初始化。

23、抽象方法

Which will declare a method that forces a subclass(子类)to implement it?
A. Public double methoda();
B. Static void methoda (double d1) {}
C. Public native double methoda();
D. Abstract public void methoda();//c抽象方法
E. Protected void methoda (double d1){}
子类必须实现父类的所有抽象方法

24、权限修饰符

您希望任何包中的子类都可以访问父类的成员。哪个访问修饰符限制最严格,可以实现这个目标?
A. Public
B. Private
C. Protected
D. Transient
E. No access modifier is qualified

private:同一个类						(default):同一个包  
(protected):不同包子类				(public):不同包非子类

25、重写

abstract class abstrctIt {
    abstract float getFloat ();
}
public class AbstractTest extends abstrctIt{
    private float f1=1.0f;
    private float getFloat(){
        return f1;
    }
}
重写原则:子类访问级别不能低于父类
原本default在同一个包下
而private则在同一个类下 访问级别降低 

26、static

package SCJ147;

public class demo26 {
    public int aMethod(){
        static int i=0;
        i++;
        return i;
    }
    public static void main(String[] args) {
        demo26 test=new demo26();
        test.aMethod();
        int j=test.aMethod();
        System.out.println(j);
    }
}

在方法内部不能定义静态变量,故编译无法通过

27、重写与重载

public class Super {
    public float getNum() {return 3.0f;}
}
public class sub extends Super{

}
Which method, placed at line 6, will cause a compiler error?
A. public float getNum()   {return 4.0f; }//成功重写
B. public void getNum ()  { }//重写 但是返回值不一样 报错
C. public void getNum (double d)   { }//重载
D. public double getNum (float d) {retrun 4.0f; }//重载

28、抽象类与final

哪个声明阻止创建外部类的子类?
A. static class FooBar{}
B. private class FooBar{}
C. abstract public class FooBar{}
D. final public class FooBar{}
E. final abstract class FooBar{}

抽象类不能被final修饰,被final修饰不能被继承。故选D

29、数组的创建

public class demo29 {
    byte [] arry1, array2[];//1为一维数组2为二维数组
    byte[][] array4;
    public static void main(String[] args) {
    }
}

30、super

public class demo30 {
    public int i=0;
    public demo30 (String text) {
        i = 16;
    }
}
public class demo30s extends demo30{
    public demo30s(String text){
       // super(text);
        i=2;
    }

    public static void main(String[] args) {
        demo30s sb=new demo30s("Hello");
        System.out.println(sb.i);
    }
}
子类的构造方法中默认第一句为super();
如果父类中没有空参构造方法,则会出现编译错误。

31、基本数据类型的转换

public class demo31 {
     double methodA(byte x, double y){
        return (short) x/y * 2;
    }
}
①参加的数据类型不同时,会按照数据长度增加的方式转换,以保证精度不降低。
②所有浮点型运算都是以双精度进行的,一个float也会转换成double型。	

36、

以下哪项获取父目录文件的名称“文件.txt”?   B
A. String name= File.getParentName(“file.txt”);
B. String name= (new File(“file.txt”)).getParent();
C. String name = (new File(“file.txt”)).getParentName();
D. String name= (new File(“file.txt”)).getParentFile();
E. Directory dir=(new File (“file.txt”)).getParentDir();
	String name= dir.getName();

37、OutputStreamWriter

哪个可以用来编码字符输出?B
A. java.io.OutputStream
B. java.io.OutputStreamWriter
C. java.io.EncodeOutputStream
D. java.io.EncodeWriter
E. java.io.BufferedOutputStream

39、DataOutputStream

Which constructs a DataOutputStream?

A. new dataOutputStream(“out.txt”);
B. new dataOutputStream(new file(“out.txt”));
C. new dataOutputStream(new writer(“out.txt”));
D. new dataOutputStream(new FileWriter(“out.txt”));
E. new dataOutputStream(new OutputStream(“out.txt”));
F. new dataOutputStream(new FileOutputStream(“out.txt”));

40、OutputStream

What writes the text “<end>” to the end of the file “file.txt”?
A. OutputStream out= new FileOutputStream (“file.txt”);
	Out.writeBytes (<end>/n”);
B. OutputStream os= new FileOutputStream (“file.txt”, true);
	DataOutputStream out = new DataOutputStream(os);
	out.writeBytes (<end>/n”);
C. OutputStream os= new FileOutputStream (“file.txt”);
	DataOutputStream out = new DataOutputStream(os);
	out.writeBytes (<end>/n”);//true为参代表可追加
D. OutputStream os= new OutputStream (“file.txt”, true);
	DataOutputStream out = new DataOutputStream(os);
	out.writeBytes (<end>/n”);//OutputStream为接口 不可new对象

41、对象回收

public class demo41 {
    public Object m(){
        Object o = new Float (3.14F);
        Object [] oa = new Object [1];
        oa[0]= o;//oa[0]也指向Float这个对象
        o = null;//o指向null
        return oa[0];
    }
    public static void main(String[] args) {
    }
    //最后由oa[0]指向Float,所以该对象不会被回收
}
当对象没有被变量指到的时候,该对象会被回收。

42、String的不变性

string foo = “ABCDE”;
foo.substring(3);
foo.concat(“XYZ”); 
String不变不变 一种为ABCDE

43、Switch

	switch (i)  {
		default:
 		System.out.printIn(“Hello”);
 }

switch中的变量是任何整数类型( char 、有符号或无符号整数,或枚举)表达式 。

51、异常处理

public class demo51 {
    public static void main(String[] args) throws IOException {
        try {
            demo51 m = new demo51();
            methodA();
        } catch (IOException e) {
            System.out.println("Caught IOException");
        } catch (Exception e) {
            System.out.println("Caught Exception");
        }
    }

    public static void methodA()  {//throws Exception 进行抛出!!!
        throw new IOException();
    }
}

52、异常处理

package SCJ147;

public class demo52 {
    public static String output = "";

    public static void foo(int i) {
        try {
            if (i ==1)
            {
                throw new Exception();
            }
            output += "1";
        } catch (Exception e) {
            output += "2";
            return;//如果没有return则输出134234
        } finally {
            output +="3";
        }
        output +="4";
    }
    public static void main(String[] args) {
        foo(0);  foo(1);
        System.out.println(output);
    }

}
输出为13423
try中产生异常后,则不会继续处理try后面的语句
如果成功解决异常,则会执行try_catch_finally后的语句
否则只执行finally的语句。

53、通过Runnable接口创建线程

public  class demo53 implements Runnable{

    public void run (Thread t) {
        System.out.println("Running.");
    }//去掉Thread t
    public static void main(String[] args) {
        new Thread (new demo53()).start();
    }
}
重写的run方法中没有参数!!!

70、静态内部类

70. Which statement about static inner classes is true?

A. An anonymous class can be declared as static.
B. A static inner class cannot be a static member of the outer class
C. A static inner class does not require an instance of the enclosing class.
D. Instance members of a static inner class can be referenced using the class name of the static inner class.
静态内部类不需要外部类的实例对象来创建。

82、字符流与字节流

Which two create an InputStream and open file the “file.txt” for reading? (Choose Two) 

A. InputStream in=new FileReader(“file.txt”);
B. InputStream in=new FileInputStream(“file.txt”);
C. InputStream in=new InputStreamFileReader (“file.txt”, “read”);
D. FileInputStream in=new FileReader(new File(“file.txt”));
E. FileInputStream in=new FileInputStream(new File(“file.txt”));   

B,E.
只有B和E new后面跟着的类为前面的子类

88、字符串的初始化

public class demo88 {
    public static void main(String[] args) {
        String[] s=new String[3];
        int dex=1;
        System.out.println(s[1]);
    }
}
结果为:null
字符串未初始化默认赋值null

111 、==比较

1. public class Foo {
2. private int val;
3. public foo(int v) (val = v;)  }
4. public static void main (String [] args)  {
5. Foo a = new Foo (10);
6. Foo b = new Foo (10);
7. Foo c = a;
8. int d = 10;
9. double e = 10.0;
10. }
11. }

Which three logical expressions evaluate to true? (Choose Three)  

A.(a ==c)
B.(d ==e)
C.(b ==d)
D.(a ==b)
E.(b ==c)
F.(d ==10.0)

A,B,F,有基本数据类型的比较都会先转为高位的基本数据类型,在比较值。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值