1.是不是一個父類寫了一個virtual 函數,如果子類覆蓋它的函數不加virtual ,也能實現多態?
--------------------------------------------------------------------------
2.輸入一個字符串,將其逆序後輸出。(使用C++,不建議用僞碼)
using namespace std;
void main()
{
char a[50];memset(a,0,sizeof(a));
int i=0,j;
char t;
cin.getline(a,50,'/n');
for(i=0,j=strlen(a)-1;i<strlen(a)/2;i++,j--)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
cout<<a<<endl;
}
cin>>str;
str.replace;
cout<<str;
--------------------------------------------------------------------------
3.請簡單描述Windows內存管理的方法。
我先說個大概,希望能夠拋磚引玉吧
--------------------------------------------------------------------------
4.
#include "stdafx.h"
#define SQR(X) X*X
{
int a = 10;
int k = 2;
int m = 1;
printf("%d/n",a);
}
這道題目的結果是什麼啊?
=>a /= (k+m)*1*(k+m);
=>a = a/9;
=>a = 1;
5.
const 符號常量;
(1)const char *p
(2)char const *p
(3)char * const p
說明上面三種描述的區別;
如果const位於星號的左側,則const就是用來修飾指針所指向的變量,即指針指向爲常量;
如果const位於星號的右側,const就是修飾指針本身,即指針本身是常量。
const char *p;
char c1='a';
char c2='b';
p=&c1;//ok
p=&c2;//ok
*p=c1;//error
(3)char * const p
這種是地址及指向對象都不能修改。
6.下面是C語言中兩種if語句判斷方式。請問哪種寫法更好?爲什麼?
int n;
if (n == 10) // 第一種判斷方式
if (10 == n) // 第二種判斷方式
7.下面的代碼有什麼問題?
void DoSomeThing(...)
{
char* p;
...
p = malloc(1024); // 分配1K的空間
if (NULL == p)
return;
...
p = realloc(p, 2048); // 空間不夠,重新分配到2K
if (NULL == p)
return;
...
}
p = malloc(1024); 應該寫成: p = (char *) malloc(1024);
沒有釋放p的空間,造成內存泄漏。
--------------------------------------------------------------------------
8.下面的代碼有什麼問題?並請給出正確的寫法。
void DoSomeThing(char* p)
{
char str[16];
int n;
assert(NULL != p);
sscanf(p, "%s%d", str, n);
if (0 == strcmp(str, "something"))
{
...
}
}
sscanf(p, "%s%d", str, n); 這句該寫成: sscanf(p, "%s%d", str, &n);
9.下面代碼有什麼錯誤?
Void test1()
{
char string[10];
char *str1="0123456789";
strcpy(string, str1);
}
10.下面代碼有什麼問題?
Void test2()
{
char string[10], str1[10];
for(i=0; i<10;i++)
{
str1[i] ='a';
}
strcpy(string, str1);
}
11.下面代碼有什麼問題?
Void test3(char* str1)
{
char string[10];
if(strlen(str1)<=10)
{
strcpy(string, str1);
}
}
==strcpy拷貝的結束標誌是查找字符串中的/0 因此如果字符串中沒有遇到/0的話會一直複製,直到遇到/0,上面的123都因此產生越界的情況
建議使用 strncpy 和 memcpy
12.下面代碼有什麼問題?
{
static int SRM_no; //是不是這裏沒賦初值?
int I;
for(I=0;I<MAX_SRM;I++,SRM_no++)
{
SRM_no %= MAX_SRM;
if(MY_SRM.state==IDLE)
{
break;
}
}
if(I>=MAX_SRM)
return (NULL_SRM);
else
return SRM_no;
}
13.寫出運行結果:
{// test1
char str[] = "world"; cout << sizeof(str) << ": ";
char *p = str; cout << sizeof(p) << ": ";
char i = 10; cout << sizeof(i) << ": ";
void *pp = malloc(10); cout << sizeof(p) << endl;
}
14.寫出運行結果:
{// test2
union V {
struct X {
unsigned char s1:2;
unsigned char s2:3;
unsigned char s3:3;
} x;
} v;
printf("%d", v.x.s3);
15.用C++寫個程序,如何判斷一個操作系統是16位還是32位的?不能用sizeof()函數
16位的系統下,
int i = 65536;
cout << i; // 輸出0;
int i = 65535;
cout << i; // 輸出-1;
int i = 65536;
cout << i; // 輸出65536;
int i = 65535;
cout << i; // 輸出65535;
if( a>65536 )
{
cout<<"32 bit"<<endl;
}
else
{
cout<<"16 bit"<<endl;
}
--------------------------------------------------------------------------
16.C和C++有什麼不同?
c++編寫面向對象的程序比c容易
c來說擴充的東西太多了,所以就在c後面放上兩個+;於是就成了c++
C++側重於對象而不是過程,側重於類的設計而不是邏輯的設計。
17.在不用第三方參數的情況下,交換兩個參數的值
#include <stdio.h>
{
int i=60;
int j=50;
i=i+j;
j=i-j;
i=i-j;
printf("i=%d/n",i);
printf("j=%d/n",j);
}
i^=j;
j^=i;
i^=j;
// 用加減實現,而且不會溢出
a = a+b-(b=a)
18.有關位域的面試題(爲什麼輸出的是一個奇怪的字符)
'b' & 0xf -->>00000010
所以輸出Ascii碼爲2的特殊字符
char t:4;就是4bit的字符變量,同樣
unsigned short i:8;就是8bit的無符號短整形變量
19.int i=10, j=10, k=3; k*=i+j; k最後的值是?
20.進程間通信的方式有?
21.
struct A
{
char t:4;
char k:4;
unsigned short i:8;
unsigned long m;
}
sizeof(A)=?(不考慮邊界對齊)
{
unsigned character : 8; // 00000000 ????????
unsigned foreground : 3; // 00000??? 00000000
unsigned intensity : 1; // 0000?000 00000000
unsigned background : 3; // 0???0000 00000000
unsigned blink : 1; // ?0000000 00000000
} screen[25][80]; // Array of bit fields
二、位結構
位結構是一種特殊的結構, 在需按位訪問一個字節或字的多個位時, 位結構
比按位運算符更加方便。
位結構定義的一般形式爲:
struct位結構名{
數據類型變量名: 整型常數;
數據類型變量名: 整型常數;
} 位結構變量;
其中: 數據類型必須是int(unsigned或signed)。整型常數必須是非負的整
數, 範圍是0~15, 表示二進制位的個數, 即表示有多少位。
變量名是選擇項, 可以不命名, 這樣規定是爲了排列需要。
例如: 下面定義了一個位結構。
struct{
unsigned incon: 8; /*incon佔用低字節的0~7共8位*/
unsigned txcolor: 4;/*txcolor佔用高字節的0~3位共4位*/
unsigned bgcolor: 3;/*bgcolor佔用高字節的4~6位共3位*/
unsigned blink: 1; /*blink佔用高字節的第7位*/
}ch;
位結構成員的訪問與結構成員的訪問相同。
例如: 訪問上例位結構中的bgcolor成員可寫成:
ch.bgcolor
注意:
1. 位結構中的成員可以定義爲unsigned, 也可定義爲signed, 但當成員長
度爲1時, 會被認爲是unsigned類型。因爲單個位不可能具有符號。
2. 位結構中的成員不能使用數組和指針, 但位結構變量可以是數組和指針,
如果是指針, 其成員訪問方式同結構指針。
3. 位結構總長度(位數), 是各個位成員定義的位數之和, 可以超過兩個字
節。
4. 位結構成員可以與其它結構成員一起使用。
例如:
struct info{
char name[8];
int age;
struct addr address;
float pay;
unsigned state: 1;
unsigned pay: 1;
}workers;
上例的結構定義了關於一個工人的信息。其中有兩個位結構成員, 每個位結
構成員只有一位, 因此只佔一個字節但保存了兩個信息, 該字節中第一位表示工
人的狀態, 第二位表示工資是否已發放。由此可見使用位結構可以節省存貯空間。
--------------------------------------------------------------------------
22.下面的函數實現在一個固定的數上加上一個數,有什麼錯誤,改正
int add_n(int n)
{
static int i=100;
i+=n;
return i;
}
因爲static使得i的值會保留上次的值。
去掉static就可了
23.下面的代碼有什麼問題?
class A
{
public:
A() { p=this; }
~A() { if(p!=NULL) { delete p; p=NULL; } }
};
會引起無限遞歸
24.
union a {
int a_int1;
double a_double;
int a_int2;
};
{
a a1;
char y;
} b;
{
double c_double;
b b1;
a a2;
VC6環境下得出的結果是32
我(sun)在VC6.0+win2k下做過試驗:
short - 2
int-4
float-4
double-8
指針-4
sizeof是C語言的一種單目操作符,如C語言的其他操作符++、--等。它並不是函數。sizeof操作符以字節形式給出了其操作數的存儲大小。操作數可以是一個表達式或括在括號內的類型名。操作數的存儲大小由操作數的類型決定。
1、用於數據類型
sizeof操作符的結果類型是size_t,它在頭文件
sizeof的優先級爲2級,比/、%等3級運算符優先級高。它可以與其他操作符一起組成表達式。如i*sizeof(int);其中i爲int類型變量。
1、sizeof操作符的一個主要用途是與存儲分配和I/O系統那樣的例程進行通信。例如:
由於操作數的字節數在實現時可能出現變化,建議在涉及到操作數字節大小時用sizeof來代替常量計算。
=============================================================
本文主要包括二個部分,第一部分重點介紹在VC中,怎麼樣採用sizeof來求結構的大小,以及容易出現的問題,並給出解決問題的方法,第二部分總結出VC中sizeof的主要用法。
對齊方式(變量存放的起始地址相對於結構的起始地址的偏移量)
偏移量必須爲sizeof(char)即1的倍數
偏移量必須爲sizeof(int)即4的倍數
偏移量必須爲sizeof(float)即4的倍數
偏移量必須爲sizeof(double)即8的倍數
偏移量必須爲sizeof(short)即2的倍數
各成員變量在存放的時候根據在結構中出現的順序依次申請空間,同時按照上面的對齊方式調整位置,空缺的字節VC會自動填充。同時VC爲了確保結構的大小爲結構的字節邊界數(即該結構中佔用最大空間的類型所佔用的字節數)的倍數,所以在爲最後一個成員變量申請空間後,還會根據需要自動填充空缺的字節。
所以該結構總的大小爲:sizeof(MyStruc)爲1+7+8+4+4=24。其中總的有7+4=11個字節是VC自動填充的,沒有放任何有意義的東西。
VC對結構的存儲的特殊處理確實提高CPU存儲變量的速度,但是有時候也帶來了一些麻煩,我們也屏蔽掉變量默認的對齊方式,自己可以設定變量的對齊方式。
以上爲sizeof的基本用法,在實際的使用中要注意分析VC的分配變量的分配策略,這樣的話可以避免一些錯誤。
--------------------------------------------------------------------------
25.i最後等於多少?
int i = 1;
int j = i++;
if((i>j++) && (i++ == j)) i+=j;
i = 5
26.
unsigned short array[]={1,2,3,4,5,6,7};
int i = 3;
*(array + i) = ?
4
27.
class A
{
virtual void func1();
void func2();
}
Class B: class A
{
void func1(){cout << "fun1 in class B" << endl;}
virtual void func2(){cout << "fun2 in class B" << endl;}
}
A, A中的func1和B中的func2都是虛函數.
B, A中的func1和B中的func2都不是虛函數.
C, A中的func2是虛函數.,B中的func1不是虛函數.
D, A中的func2不是虛函數,B中的func1是虛函數.
A
28.
數據庫:抽出部門,平均工資,要求按部門的字符串順序排序,不能含有"human resource"部門,
select depart_name, avg(wage)
from employee
where depart_name <> 'human resource'
group by depart_name
order by depart_name
29.
給定如下SQL數據庫:Test(num INT(4)) 請用一條SQL語句返回num的最小值,但不許使用統計功能,如MIN,MAX等
select top 1 num
from Test
order by num desc
30.
輸出下面程序結果。
{
public:
virtual void print(void)
{
cout<<"A::print()"<<endl;
}
};
class B:public A
{
public:
virtual void print(void)
{
cout<<"B::print()"<<endl;
};
};
class C:public B
{
public:
virtual void print(void)
{
cout<<"C::print()"<<endl;
}
};
void print(A a)
{
a.print();
}
void main(void)
{
A a, *pa,*pb,*pc;
B b;
C c;
pa=&a;
pb=&b;
pc=&c;
a.print();
b.print();
c.print();
pa->print();
pb->print();
pc->print();
print(a);
print(b);
print(c);
}
A::print()
B::print()
C::print()
A::print()
B::print()
C::print()
A::print()
A::print()
A::print()
31.
試編寫函數判斷計算機的字節存儲順序是開序(little endian)還是降序(bigendian)
bool IsBigendian()
{
unsigned short usData = 0x1122;
unsigned char *pucData = (unsigned char*)&usData;
}
32.簡述Critical Section和Mutex的不同點
對幾種同步對象的總結
1.Critical Section
A.速度快
B.不能用於不同進程
C.不能進行資源統計(每次只可以有一個線程對共享資源進行存取)
A.速度慢
B.可用於不同進程
C.不能進行資源統計
A.速度慢
B.可用於不同進程
C.可進行資源統計(可以讓一個或超過一個線程對共享資源進行存取)
A.速度慢
B.可用於不同進程
C.可進行資源統計
--------------------------------------------------------------------------
33.一個數據庫中有兩個表:
一張表爲Customer,含字段ID,Name;
一張表爲Order,含字段ID,CustomerID(連向Customer中ID的外鍵),Revenue;
寫出求每個Customer的Revenue總和的SQL語句。
create table customer
(
ID int primary key,Name char(10)
)
(
ID int primary key,CustomerID int foreign key references customer(id) , Revenue float
)
select Customer.ID, sum( isnull([Order].Revenue,0) )
from customer full join [order]
on( [order].customerid=customer.id )
group by customer.id
34.請指出下列程序中的錯誤並且修改
void GetMemory(char *p){
p=(char *)malloc(100);
}
void Test(void){
char *str=NULL;
GetMemory=(str);
strcpy(str,"hello world");
printf(str);
}
GetMemory並不能傳遞動態內存,Test函數中的 str一直都是 NULL。
strcpy(str, "hello world");將使程序崩潰。
char *GetMemory(){
char *p=(char *)malloc(100);
return p;
}
void Test(void){
char *str=NULL;
str=GetMemory(){
strcpy(str,"hello world");
printf(str);
}
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
35.程序改錯
class mml
{
private:
static unsigned int x;
public:
mml(){ x++; }
mml(static unsigned int &) {x++;}
~mml{x--;}
pulic:
virtual mon() {} = 0;
static unsigned int mmc(){return x;}
......
};
class nnl:public mml
{
private:
static unsigned int y;
public:
nnl(){ x++; }
nnl(static unsigned int &) {x++;}
~nnl{x--;}
public:
virtual mon() {};
static unsigned int nnc(){return y;}
......
};
mml* pp = new nnl;
..........
delete pp;
A:
基類的析構函數應該爲虛函數
virtual ~mml{x--;}
36.101個硬幣100真、1假,真假區別在於重量。請用無砝碼天平稱兩次給出真幣重還是假幣重的結論。
101個先取出2堆,
33,33
第一次稱,如果不相等,說明有一堆重或輕
那麼把重的那堆拿下來,再放另外35箇中的33
如果相等,說明假的重,如果不相等,新放上去的還是重的話,說明假的輕(不可能新放上去的輕)
下面就不用說了
第3題也可以拿A(50),B(50)比一下,一樣的話拿剩下的一個和真的比一下。
如果不一樣,就拿其中的一堆。比如A(50)再分成兩堆25比一下,一樣的話就在
B(50)中,不一樣就在A(50)中,結合第一次的結果就知道了。
37.static變量和static 函數各有什麼特點?
static變量:在程序運行期內一直有效,如果定義在函數外,則在編譯單元內可見,如果在函數內,在在定義的block內可見;
static函數:在編譯單元內可見;
38.用C 寫一個輸入的整數,倒着輸出整數的函數,要求用遞歸方法 ;
void fun( int a )
{
printf( "%d", a%10 );
a /= 10;
if( a <=0 )return;
}
39.寫出程序結果:
void Func(char str[100])
{
printf("%d/n", sizeof(str));
}
4
分析:
指針長度
40.int id[sizeof(unsigned long)];
這個對嗎?爲什麼??
對
這個 sizeof是編譯時運算符,編譯時就確定了
可以看成和機器有關的常量。
本文主要包括二個部分,第一部分重點介紹在VC中,怎麼樣採用sizeof來求結構的大小,以及容易出現的問題,並給出解決問題的方法,第二部分總結出VC中sizeof的主要用法。
對齊方式(變量存放的起始地址相對於結構的起始地址的偏移量)
偏移量必須爲sizeof(char)即1的倍數
偏移量必須爲sizeof(int)即4的倍數
偏移量必須爲sizeof(float)即4的倍數
偏移量必須爲sizeof(double)即8的倍數
偏移量必須爲sizeof(short)即2的倍數
各成員變量在存放的時候根據在結構中出現的順序依次申請空間,同時按照上面的對齊方式調整位置,空缺的字節VC會自動填充。同時VC爲了確保結構的大小爲結構的字節邊界數(即該結構中佔用最大空間的類型所佔用的字節數)的倍?