用命令方式运行以下代码的运行结果是()
public class f{ public static void main(String[] args){ String foo1 = args[1]; String foo2 = args[2]; String foo3 = args[3]; } }
命令: java f a b c
A. 程序编译错误
B. a b c
C. 程序运行错误
D. f
答案:C
若有定义语句: int a=10 ; double b=3.14 ; 则表达式 'A'+a+b 值的类型是()
A. char
B. int
C. double
D. float
答案:C
指出下列程序运行的结果()
public class Example{ String str = new String("good"); char[ ] ch = { 'a' , 'b' , 'c' }; public static void main(String args[]){ Example ex = new Example(); ex.change(ex.str,ex.ch); System.out.print(ex.str + " and "); System.out.print(ex.ch); } public void change(String str,char ch[ ]){ str = "test ok"; ch[0] = 'g'; } }
A. good and abc
B. good and gbc
C. test ok and abc
D. test ok and gbc
答案:B
下列说法正确的是
A. 在类方法中可用this来调用本类的类方法
B. 在类方法中调用本类的类方法可直接调用
C. 在类方法中只能调用本类的类方法
D. 在类方法中绝对不能调用实例方法
答案:B
可将语句块或方法设为同步使用的语句是()
A. synchronized
B. static
C. abstract
D. final
答案:A
已知如下类说明:
public class Test{ private float f=1.0f; int m=12; static int n=1; public static void main(String args[]){ Test t=new Test(); } }
如下哪些使用是正确的()
A. t.f = 1.0
B. this.n
C. Test.m
D. Test.n
答案:D
有以下代码:
class A{ public A(String str){ } } public class Test{ public static void main(String[] args) { A classa=new A("he"); A classb=new A("he"); System.out.println(classa==classb); } }
请问输出的结果是:
A. false
B. true
C. 报错
D. 以上选项都不正确
答案:A
以下哪项不属于java类加载过程?
A. 生成java.lang.Class对象
B. int类型对象成员变量赋予默认值
C. 执行static块代码
D. 类方法解析
答案:B
如果一个方法或变量是"private"访问级别,那么它的访问范围是:
A. 在当前类,或者子类中
B. 在当前类或者它的父类中
C. 在当前类,或者它所有的父类中
D. 在当前类中
答案:D
java中下面哪个能创建并启动线程()
public class MyRunnable implements Runnable { public void run() { //some code here } }
A. new Runnable(MyRunnable).start()
B. new Thread(MyRunnable).run()
C. new Thread(new MyRunnable()).start()
D. new MyRunnable().start()
答案:C
不用加减乘除做加法
题目描述:写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
数据范围:两个数都满足-10<=n<=1000
进阶:空间复杂度 O(1),时间复杂O(1)
public class Solution4 { public int Add(int num1,int num2){ while (num2 !=0){ int a=num1 ^num2; int b=(num1 & num2)<<1; num1=a; num2=b; } return num1; } }
三角形
题目描述:给定三条边,请你判断一下能不能组成一个三角形。
输入描述:输入包含多组数据,每组数据包含三个正整数a、b、c(1≤a, b, c≤10^100)。
输出描述:对应每一组数据,如果它们能组成一个三角形,则输出“Yes”;否则,输出“No”。
public class Main43 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); while (scanner.hasNext()){ BigDecimal a=scanner.nextBigDecimal(); BigDecimal b=scanner.nextBigDecimal(); BigDecimal c=scanner.nextBigDecimal(); if (a.add(b).compareTo(c) >0 && a.add(c).compareTo(b) >0 && b.add(c).compareTo(a) >0){ System.out.println("Yes"); }else { System.out.println("No"); } } } }
反转部分单链表
题目描述:给定一个单链表,在链表中把第 L 个节点到第 R 个节点这一部分进行反转。
输入描述:n 表示单链表的长度。 val 表示单链表各个节点的值。L 表示翻转区间的左端点。 R 表示翻转区间的右端点。
输出描述:在给定的函数中返回指定链表的头指针。
public class Main44 { //首先定义单链表的节点 static class Node{ int val; Node next; public Node(int val){ this.val=val; } } public static void main(String[] args) { Scanner scanner=new Scanner(System.in); //获取当前链表的节点个数 int n=scanner.nextInt(); //吃掉nextInt方法的换行符 scanner.nextLine(); // "1 2 3 4 5" String nodeValue=scanner.nextLine(); String[] nodes=nodeValue.split(" "); //进行链表的创建 Node dummyHead=new Node(-1); Node tail=dummyHead; for (int i = 0; i < n; i++) { //链表的尾插 Node node=new Node(Integer.parseInt(nodes[i])); tail.next=node; tail=node; } //获取左区间和右区间的范围 "1 3" String part=scanner.nextLine(); //1 int left=Integer.parseInt(part.split(" ")[0]); //3 int right=Integer.parseInt(part.split(" ")[1]); Node newHead=reversePartList(dummyHead.next,left,right); //进行输出处理 while (newHead !=null){ System.out.print(newHead.val+" "); newHead=newHead.next; } } public static Node reversePartList(Node head,int left,int right){ Node dummyHead=new Node(-1); dummyHead.next=head; Node prev=dummyHead; for (int i = 1; i < left; i++) { prev=prev.next; } //prev引用指向待反转区间的前驱节点,cur引用就是待反转区间的第一个节点 Node cur=prev.next; for (int i = left; i < right; i++) { Node third=cur.next; //先把third引用从链表中删除 cur.next=third.next; //再把third节点插入prev中 third.next=prev.next; prev.next=third; } return dummyHead.next; } }