1、假定int整形指针p所指对象的值为25,p+1所指对象的值为46,执行(*p)++后,p所指对象的值为?
答:26
int *p;
int a[2] = {25,46};
p = a;
cout<<*p<<endl;
return 0;
2、某人上楼梯,1步可以跨一个台阶或2个台阶,这个楼梯共有10个台阶,从地面到最上层共有多少种不同跨法?
答案:共89种
只一次两个台阶 有c(1/9)=9种
2次两个台阶 有c(2/8)=28种
3次两个台阶 有C(3/7)=35种
4次两个台阶 C(4/6)=15种
5次两个台阶 1种
0次两个台阶 1种
3、下列说明中 const char *ptr;ptr应该是()
A 指向字符常量的指针;
B 指向字符的常量指针;
C 指向字符串常量的指针;
D 指向字符串的常量指针;
答案:A C
4、对使用关键字new所开辟的动态存储空间,释放时必须使用 delete 。
答:C++程序中由new分配的动态内存空间必须通过delete释放。
5、单链表逆序(三种方法)
public class LinkedList {
class Node {
private Node next;
private Object data;
public Node() {
super();
}
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
private Node head;
public Node first() {
return head.next;
}
public Node tail() {
Node temp = head;
while (temp.next != null)
temp = temp.next;
return temp;
}
public Node head() {
return head;
}
public LinkedList() {
head = new Node(null, null);
}
public boolean addStack(Object data, Node head) {
boolean flag = false;
Node node = new Node(data, null);
if (!flag) {
node.next = head.next;
head.next = node;
flag = true;
}
return flag;
}
public boolean addQueue(Object data, Node head) {
Node temp = head;
while (temp.next != null)
temp = temp.next;
boolean flag = false;
Node node = new Node(data, null);
if (!flag) {
temp.next = node;
flag = true;
}
return flag;
}
public void print(Node head) {
Node temp = head;
while (temp.next != null) {
System.out.println("list node data :" + temp.data);
temp = temp.next;
}
System.out.println("list node data :" + temp.data);
}
public void addQueue() {
}
//直接头结点插入
public LinkedList invertedList(Node head) {
LinkedList newList = new LinkedList();
Node temp = head;
while (temp.next != null) {
addStack(temp.data, newList.head);
temp = temp.next;
}
addStack(temp.data, newList.head);
return newList;
}
public int length() {
Node temp = head;
int i = 0;
while (temp.next != null) {
i++;
temp = temp.next;
}
return i;
}
//交换两个对应的数据来实现,链表逆序
public void invertedListExchange(Node head) {
Node tempStart = head;
Object old = null;
Node tempEnd = null;
int halfLen = this.length() / 2;
for (int i = 0; i < halfLen; i++) {
tempEnd = tempStart;
if (i == 0) {
tempEnd = this.tail();
}
while (i != 0 && tempEnd.next.data != old)
tempEnd = tempEnd.next;
System.out.println(tempStart.data);
Object tt = tempStart.data;
tempStart.data = tempEnd.data;
tempEnd.data = tt;
old = tempEnd.data;
tempStart = tempStart.next;
}
}
//利用栈的性质来实现
public LinkedList invertedListByStack(Node head) {
Deque<Object> stack = new java.util.LinkedList<Object>();
Node temp = head;
while (temp.next != null) {
stack.push(temp.data);
temp = temp.next;
}
stack.push(temp.data);
LinkedList newList = new LinkedList();
Node node = newList.head;
while (!stack.isEmpty()) {
addQueue(stack.pop(), node);
}
return newList;
}
}
6、TCP与UDP
TCP:(Transmission Control Protocol 传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议。
UDP:(User Datagram Protocol用户数据报协议)是OSI(Open System Interconnection,开放式系统互联) 参考模型中一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务。
区别: 1.基于连接与无连接
2.TCP要求系统资源较多,UDP较少;
3.UDP程序结构较简单
4.流模式(TCP)与数据报模式(UDP);
5.TCP保证数据正确性,UDP可能丢包
6.TCP保证数据顺序,UDP不保证
差别 | TCP | UDP |
是否连接 | 面向连接 | 面向非连接 |
传输可靠性 | 可靠 | 不可靠 |
应用场合 | 传输大量数据 | 少量数据 |
速度 | 慢 | 快 |
7、分别给出 BOOL,int,float,指针变量 与“零值”,比较的 if 语句(假设变量名为 var)
答:
BOOL 型变量:if(!var)
int 型变量: if(var==0)
float 型变量:
const float EPSINON = 0.00001;
if ((x >= - EPSINON) && (x <= EPSINON)
指针变量: if(var==NULL)