1.有1、2、3、4、5个数字,能组成多少个互不相同且无重复数字的四位数?都是多少?
提示:可填在千位、百位、十位、个位的数字都是1、2、3、4、5。组成所有的排列后再去 掉不满足条件的排列。
代码:
public class test01 {
public static void main(String[] args) {
int count = 0;
for(int a = 1;a<6;a++) {
for(int b=1;b<6;b++) {
for(int c =1;c<6;c++) {
for(int d=1;d<6;d++) {
if(a != b && a != c && a != d && b!=c && b!=d && c!=d) {
count++;
System.out.println(a*1000 + b*100 + c*10 + d + " ");
if(count%4 == 0) {
System.out.println();
}
}
}
}
}
}
System.out.println("共有" + count + "个符合要求的四位数");
}
}
输出:
2.鸡兔同笼,上有35头 ,下有94足,问鸡兔各几只?
public class test02 {
public static void main(String[] args) {
int chookNum = 0;
int rabitNum = 0;
for(chookNum = 0;chookNum<35;chookNum++) {
rabitNum = 35 - chookNum;
if(chookNum * 2 + rabitNum * 4 ==94) {
System.out.println("鸡" + chookNum + "只" +" "+ "兔:" + rabitNum + "只");
}
}
}
}
输出:
3.一共30人,包含男人,女人和孩子,他们去吃自助餐,共花费500元,男人30元/位,女人20元/位,小孩10元/位, 请问男人女人和小孩的组合,有哪几种,编程实现
public class test03 {
public static void main (String[] args) {
int men = 0;
int women = 0;
int childs = 0;
int count = 0;
//由 men + women + childs = 30 和 30*men + 20*women + 10*childs =500;知
//2*men + women = 20 ---0
输出:
4. int[] [] a = new int[] [4] , 问 a.length =?
答 :代码错误 ,二维数组定义出错
5.
public static void main(String[] args) {
int n=10;
for( int i=1;i<=n;i++){
if(n%i!=0)
continue;
System.out.print(i+",");
}
}
结果是 1,2,5,10,
6.
public class Test4 {
static int j =20;
public void amethod(int x){
x=x*2;
j=j*2;
}
public static void main(String[] args) {
int i=10;
Test4 test4 = new Test4();
test4.amethod(i);
System.out.println("i="+i+",j="+j);
}
}
输出:i=10,j=40
7.
public class Parent {
void printMe(){
System.out.println("parent");
}
}
class Child extends Parent{
void printMe(){
System.out.println("child");
}
void printAll(){
super.printMe();
this.printMe();
printMe();
}
}
public class Test5 {
public static void main(String[] args) {
Child child = new Child();
child.printAll();
}
}
输出结果:
parent
child
child
8.
public class Test6 {
public static void bMethod(){
}
public static void main(String[] args) {
try {
bMethod();
System.out.println("A");
} catch (Exception e) {
System.out.println("B");
}finally{
System.out.println("C");
}
System.out.println("D");
}
}
最终输出: ACD
9.
public class Test7 {
private static final int[] a={10};
public static void main(String[] args) {
a[0]=20;
System.out.println(a);
}
}
正确 ,打印了数组在jvm中的虚拟内存地址
10. 在Java中如何实现重载 , 重载的规则是什么?
在一个类中 ,定义相同的方法名 ,不同的参数既可以实现重载了
重载规则
11. java垃圾回收机制
垃圾回收是jvm中一个优先级很低的后台线程 ,它在空闲时自动调用 ,回收不用的变量
没用的变量的定义条件 : 1.对象的引用是null的, 2.一次性使用的匿名对象
12.java中 ,类变量 ,实例变量,成员变量,局部变量,都是怎么定义的 ,区别在哪 ,作用域都是什么
类变量 : 也叫静态变量 ,是被static修饰的变量 ,它在类载入是创建 ,垃圾回收时销毁
实例变量: 是类中的成员变量 ,在类被初始化后,才存在 ,非static修饰的
成员变量 : 类的属性 , 被类的所有方法共享 ,作用域为整个类
局部变量 : 类的方法中声明的变量 ,局部变量只在方法中有效,方法结束了,局部变量就失效了 ,作用域为当前方法
13 . private default protected public
同一个类 有效 有效 有效 有效
同一个包中的类 有效 有效 有效
不同包的子类 有效 有效
不同包的其他类 有效
14. java中 ,static 可以修饰那些元素?
Static 可以修饰
属性 (类变量, 略 , 使用 类.变量 或者 对象.变量 ,好处:类不用实例化也可以调用,简化代码)
方法( 类方法,静态方法,不需要实例化就可以直接访问, 使用 类.方法名() 或者 对象.方法名())
块 (静态块 , 类加载时静态块先加载,而且只加载一次 ,多次的实例化对象时,静态块只在第一次实例化对象时加载,以后就不加载了)
18.java中 , 子类在实例化过程中,子类构造器要求是什么?
子类在实例化时, 必须调用父类的构造器
如果子类是无参数的实例化 , 调用 父类对应的无参数构造器 ,如果父类中没有无参数构造器,子类的实例化会出错
子类可以在自己的构造器中使用super关键字 ,调用父类的构造器 ,super (arg1,arg2,….)方法应该放在构造器的第一行
19.final void aa() { …} 最终方法 ,子类不能重写该方法
20. int I ; 还没有赋初始化值 ,如果是成员变量 ,默认值是0 , 局部变量 ,需要为它赋值后才能使用
Boolean b 如果是成员变量 ,默认值是false
21. int[] m= new int[4] ; m[4] =10; indexOutOfBoundException 或者Exception 都可以捕获它的错误
22. abstract 抽象关键字 ,可以用在方法和类上 , 但是变量和构造方法不能用abstract修饰 , 如果一个方法是abstract修饰 ,那么类也应该是被abstract修饰的抽象类
23. byte 1 字节 -128~127
Short 2 字节 -32768 ~32767
Int 4 字节 -2147483648~2147483647 10位
Long 8 字节 -9223372036854775808~9223372036854775807 19位整数
24. int[] arrs =sort( new Stu( 12 ));-- 调用形式
该方法定义如下 public int[] sort(Stu s){ ……}