1、What gets printed()(5’)
counter = 1
def doLotsOfStuff():
global counter
for i in (1,2,3):
counter+=1
doLotsStuff()
print counter
A.1 B.3 *C.4 D.7 E.none of the above
2、What gets printed()(5’)
numbers = [1,2,3,4]
numbers.append([5,6,7,8])
numbers.extend([9,10])
print len(numbers)
A.4 B.3 *C.7 D.10 E.6 F.An exception is throw
3、What gets printed()(5’)
key2 = {'1':1,'2':[2,4]}
TheCopy = kvps.copy()
kvps['1'] = 5
kvps['2'][1] = 8
sum = kvps['2'][1]+theCopy['2'][1]+theCopy['1']
print sum
A.21 B.13 *C.17 D.15 E.An exception is throw
4、What gets printed()(5’)
class A(object):
x = 100
def __init__(self,value):
self.x = value
a = A(1000)
print a.x+A.x
A.200 *B.1100 C.2000 D.An exception is throw
5、What gets printed()(5’)
def add(a,b):
return a+b
x = 0
try:
add('a',1)
except TypeError:
x |=1<<7
else:
x |=1<<6
finally hex(x)
*A.0x81 B.0x1 C.0xc1 D.0x80 E.6 F.Anexception is thrown
6、给定一个整型变量a,写出三个表达式,第一个设置a的第四个bit(bit3)为1,第二个清除a的第四个bit(bit3)为0.第三个将a的第四个bit(bit3)取反。在以上操作中,要保持其他位不变(10’)。
a = a&(1<<3)
a = a&(~(1<<3))
a =
7、Given following C cod, please design proper test flow to cover as many brances as possible(15’).
int GetMaxDay(int year,int month)
{
int maxday = 0;
if(month>=1 && month<=12)
{
if(month == 2)
{
if(year%4 == 0)
{
if(year%100 == 0)
{
if(year%400 == 0)
maxday = 29;
else
maxday = 28;
else
maxday = 29;
else
maxday = 29;
else
{
if(month == 4||month == 6||month==9||month == 11)
maxday = 30;
else
maxday = 31;
}
}
return maxday;
}
//参考程序员面试宝典 第五版 269页解法
8、用宏定义实现swap(x,y),即交换两个数(15’)。
#define swap(a,b) a = a+b,b=a-b,a=a-b
9、static局部变量和普通局部变量的区别(15’)。
Example:
int example(int a,int b)
{
static int c =0;
c = a+b;
return c;
}
//提示,在调用后是否会被清空、下一次调用值是否会保留上一次的结果
10、编写C/C++程序实现如何找出单链表中的倒数第K个元素(20’)
//提示:两个指针距离为K,后面一个指针指向尾部,另外一个指针所指即为所求
//参考:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k)
{
ListNode *p1,*p2;
p1 = pListHead;
p2 = pListHead;
if(pListHead == NULL||k <= 0)
return NULL;
for(int i = 0;inext!=NULL)
p2 = p2->next;
else
return NULL;
}
while(p2->next!=NULL)
{
p1 = p1->next;
p2 = p2->next;
}
return p1;
}
};