高級C語言程式設計師測試必過的十六道最佳題目+答案詳解

2020-12-11 51CTO

【引自dishening的博客】整個測試遵循以下的約定:
◆假定在所有的程序中必須的頭文件都已經被正確包含。

考慮如下的數據類型:

◆char為1個字節
◆int為4個字節
◆long int為4個字節
◆float為4個字節
◆double為個8位元組
◆long double為8個字節
◆指針為4個字節

1、Consider the following program:

#include<setjmp.h>
static jmp_buf  buf;
main()
{
volatile  int b;
b =3;
if(setjmp(buf)!=0) 
{
printf("%d ", b); 
exit(0);
}
b=5;
longjmp(buf , 1);
}

The output for this program is:

(a) 3

(b) 5

(c) 0

(d) None of the above

2、Consider the following program:

main()
{
struct node
{
int a;
int b;
int c;    
};
struct node  s= { 3, 5,6 };
struct node *pt = &s;
printf("%d" ,  *(int*)pt);
}

The output for this program is:

(a) 3

(b) 5

(c) 6

(d) 7

3、Consider the following code segment:

int  foo ( int x , int  n)
{
int val;
val =1;
if (n>0)
{
if (n%2 == 1)  val = val *x;
val = val * foo(x*x , n/2);
}
return val;
}

What function of x and n is compute by this code segment?  
(a) x^n
(b) x*n
(c) n^x
(d) None of the above

4、Consider the following program:

main()
{
int  a[5] = {1,2,3,4,5};
int *ptr =  (int*)(&a+1);
printf("%d %d" , *(a+1), *(ptr-1) );
}

The output for this program is:

(a) 2 2

(b) 2 1

(c) 2 5

(d) None of the above

5、Consider the following program:

void foo(int [][3] );    
main()
{
int a [3][3]= { { 1,2,3} , { 4,5,6},{7,8,9}};
foo(a);
printf("%d" , a[2][1]);
}
void foo( int b[][3])
{
++ b;
b[1][1] =9;
}

The output for this program is:

(a) 8

(b) 9

(c) 7

(d) None of the above

6、Consider the following program:

main()
{
int a, b,c, d;
a=3;
b=5;
c=a,b;
d=(a,b);
printf("c=%d" ,c);
printf("d=%d" ,d);
}

The output for this program is:

(a) c=3 d=3

(b) c=5 d=3

(c) c=3 d=5

(d) c=5 d=5

7、Consider the following program:

main()
{
int a[][3] = { 1,2,3 ,4,5,6};
int (*ptr)[3] =a;
printf("%d %d "  ,(*ptr)[1], (*ptr)[2] );
++ptr;
printf("%d %d"  ,(*ptr)[1], (*ptr)[2] );
}

The output for this program is:

(a) 2 3 5 6

(b) 2 3 4 5

(c) 4 5 0 0

(d) None of the above

8、Consider following function:

int *f1(void)
{
int x =10;
return(&x);
}
int *f2(void)
{
int*ptr;
*ptr =10;
return ptr;
}
int *f3(void)
{
int *ptr;
ptr=(int*) malloc(sizeof(int));
return ptr;
}

Which of the above three functions are likely to cause problem with pointers

(a) Only f3

(b) Only f1 and f3

(c) Only f1 and f2

(d) f1 , f2 ,f3

9、Consider the following program:

main()
{
int i=3;
int j;
j = sizeof(++i+ ++i);
printf("i=%d j=%d", i ,j);
}

The output for this program is:
(a) i=4 j=2
(b) i=3 j=2
(c) i=3 j=4
(d) i=3 j=6

10、Consider the following program:

void f1(int *, int);
void f2(int *, int);
void(*p[2]) ( int *, int);
main()
{
int a;
int b;
p[0] = f1;
p[1] = f2;
a=3;
b=5;
p[0](&a , b);
printf("%d\t %d\t" , a ,b);
p[1](&a , b);
printf("%d\t %d\t" , a ,b);
}
void f1( int* p , int q)
{
int tmp;
tmp =*p;
*p = q;
q= tmp;
}
void f2( int* p , int q)
{
int tmp;
tmp =*p;
*p = q;
q= tmp;

The output for this program is:

(a) 5 5 5 5

(b) 3 5 3 5

(c) 5 3 5 3

(d) 3 3 3 3

11、Consider the following program:

void e(int );  
main()
{
int a;
a=3;
e(a);
}
void e(int n)
{
if(n>0)
{
e(--n);
printf("%d" , n);
e(--n);
}
}

The output for this program is:

(a) 0 1 2 0

(b) 0 1 2 1

(c) 1 2 0 1

(d) 0 2 1 1

12、Consider following declaration

typedef int (*test) ( float * , float*)
test tmp;

type of tmp is

(a) Pointer to function of having two arguments that is pointer to float

(b) int

(c) Pointer to function having two argument that is pointer to float and return int

(d) None of the above

13、Consider the following program:

main()
{
char *p;
char buf[10] ={ 1,2,3,4,5,6,9,8};
p = (buf+1)[5];
printf("%d" , p);
}

The output for this program is:

(a) 5

(b) 6

(c) 9

(d) None of the above

14、Consider the following program:

Void f(char**);
main()
{
char * argv[] = { "ab" ,"cd" , "ef" ,"gh", "ij" ,"kl" };
f( argv );
}
void f( char **p )
{
char* t;
t= (p+= sizeof(int))[-1];
printf( "%s" , t);
}

The output for this program is:

(a) ab

(b) cd

(c) ef

(d) gh

15、Consider the following program:

#include<stdarg.h>
int ripple ( int , ...);
main()
{
int num;
num = ripple ( 3, 5,7);
printf( " %d" , num);
}
int ripple (int n, ...)
{
int i , j;
int k; 
va_list p;
k= 0;
j = 1;
va_start( p , n);    
for (; j<n;  ++j)
{
i =  va_arg( p , int);
for (; i;    i &=i-1  )
++k;
}
return k;
}

The output for this program is:

(a) 7

(b) 6

(c) 5

(d) 3

16、Consider the following program:

int counter (int i)
{
static int count =0;
count = count +i;
return (count );
}
main()
{
int i , j;
for (i=0; i <=5; i++)
j = counter(i);
}

The value of j at the end of the execution of the this program is:

(a) 10

(b) 15

(c) 6

(d) 7

Answer With Detailed Explanation

Answer 1.
The answer is (b)

volatile variable isn't affected by the optimization. Its value after the longjump is the last value variable assumed.

b last value is 5 hence 5 is printed.

setjmp : Sets up for nonlocal goto /* setjmp.h*/

Stores context information such as register values so that the lomgjmp function can return control to the statement following the one calling setjmp.Returns 0 when it is initially called.

Lonjjmp:longjmp Performs nonlocal goto /* setjmp.h*/

Transfers control to the statement where the call to setjmp (which initialized buf) was made. Execution continues at this point as if longjmp cannot return the value 0.A

nonvolatile automatic variable might be changed by a call to longjmp.When you use setjmp and longjmp, the only automatic variables guaranteed to remain valid are those declared volatile.

Note: Test program without volatile qualifier (result may very)

Answer 2.
The answer is (a)

The members of structures have address in increasing order of their declaration. If a pointer to a structure is cast to the type of a pointer to its first member, the result refers to the first member.

Answer 3.
The answer is (a)

Non recursive version of the program

int  what ( int x , int  n)
{
int val;
int product;
product =1;
val =x;
while(n>0)
{
if (n%2 == 1) 
product = product*val;
n = n/2;
val = val* val;
}
}

/* Code raise a number (x) to a large power (n) using binary doubling strategy */

Algorithm description

(while n>0) 
{
if  next most significant binary digit of  n( power)  is one
then multiply accumulated product by current val,
reduce n(power)  sequence by a factor of two using integer division.
get next val by multiply current value of itself       
}

Answer 4.

The answer is (c)

type of a is array of int
type of &a is pointer to array of int
Taking a pointer to the element one beyond the end of an array is sure to work.

Answer 5.
The answer is (b)

Answer 6.
The answer is (c)

The comma separates the elements of a function argument list. The comma is also used as an operator in comma expressions. Mixing the two uses of comma is legal, but you must use parentheses to distinguish them. the left operand E1 is evaluated as a void expression, then E2 is evaluated to give the result and type of the comma expression. By recursion, the expression E1, E2, ..., En results in the left-to-right evaluation of each Ei, with the value and type of En giving the result of the whole expression.

c=a,b;  / *yields c=a* /
d=(a,b); /* d =b  */

Answer 7.

The answer is (a)

/* ptr is pointer to array of 3 int */

Answer 8.
The answer is (c)

f1 and f2 return address of local variable ,when function exit local variable disappeared

Answer 9.
The answer is (c)

sizeof operator gives the number of bytes required to store an object of the type of its operand . The operands is either an expression, which is not evaluated ( (++i + ++ i ) is not evaluated so i remain 3 and j is sizeof int that is 2) or a parenthesized type name.

Answer 10.
The answer is (a)

void(*p[2]) ( int *, int);
define array of pointer to function accept two argument that is pointer to int and return int. p[0] = f1; p[1] = f2 contain address of function .function name without parenthesis represent address of function Value and address of variable is passed to function only argument that is effected is a (address is passed). Because of call by value f1, f2 can not effect b

Answer 11.
The answer is (a)

Answer 12.
The answer is (c)

C provide a facility called typedef for creating new data type names, for example declaration

Makes the name string a synonym for int .The type string can be used in declaration, cast, etc, exactly the same way that the type int can be. Notice that the type being declared in a typedef appears in the position of a variable name not after the word typedef.

Answer 13.
The answer is (c)

If the type of an expression is "array of T" for some type T, then the value of the expression is a pointer to the first object in the array, and the type of the expression is altered to "pointer to T"

So (buf+1)[5] is equvalent to *(buf +6) or buf[6]

Answer 14.
The answer is (d)

p+=sizeof(int) point to argv[2]
(p+=sizeof(int))[-1] points to argv[1]

Answer 15.
The answer is (c)

When we call ripple value of the first argument passed to ripple is collected in the n that is 3. va_start initialize p to point to first unnamed argument that is 5 (first argument).Each call of va_arg return an argument and step p to the next argument. va_arg uses a type name to determine what type to return and how big a step to take Consider inner loop

(; i; i&=i-1) k++ /* count number of  1 bit in i *

in five number of 1 bits is (101) 2
in seven number of 1 bits is (111) 3
hence k return 5

example

let  i= 9 = 1001
i-1  = 1000   
(i-1) +1 = i
1000
+1
1 001

The right most 1 bit of i has corresponding 0 bit in i-1 this way i & i-1, in a two complement number system will delete the right most 1 bit I(repeat until I become 0 gives number of 1 bits)

Answer 16.
The answer is (b)

Static variable count remain in existence rather than coming and going each time function is called
so first call counter(0) count =0
second call counter(1) count = 0+1;
third call counter(2) count = 1+2; /* count = count +i */
fourth call counter(3) count = 3+3;
fifth call counter(4) count = 6+4;
sixth call counter(5) count = 10+5;

來源連結:http://tb.blog.csdn.net/TrackBack.aspx?PostId=1343232

(責任編輯 火鳳凰 sunsj@51cto.com  TEL:(010)68476636-8007)

相關焦點

  • 嵌入式系統高級C語言編程
    內容簡介  《嵌入式系統高級C語言編程》將主要介紹針對嵌入式系統的基於C語言的軟體項目開發的流程,較為複雜的c語言編程知識和技巧,編程風格和調試習慣
  • 刷爆網絡的「左右腦年齡測試」真相,程式設計師這麼說
    測試中,只要通過掃描測試結果圖中的二維碼並回答幾個設定的問題後,便會生成一張顯示有左右腦兩個年齡測試結果的圖片,圖片的下方還配有相關的文字解釋。◆  ◆  ◆  ◆  ◆就在「左右腦年齡測試」熱傳不久後,便有網友稱,同樣的答案會得到不同的分數,並質疑測試結果的真實性。同時有程式設計師表示,研究測試代碼發現,測出來的年齡其實只是隨機數。
  • 從年薪10萬到年薪30萬,C語言程式設計師必讀的5本書
    C語言可以把程序簡單地編譯為機器指令,使得它成為了最高效的語言。為什麼在程式設計師中,C語言如此流行呢?這背後有很多原因。首先,它獨立於平臺,可以運行在MAC、Linux、UNIX和PC各種環境中。它是UNIX系統以及其他同樣結構的系統的默認語言。同時,它是一種多用途的語言,它可以用於編寫設備的驅動程序、桌面應用、嵌入式系統和DBMS(資料庫管理系統)。
  • 左右腦年齡測試不靠譜 程式設計師:測試結果只是隨機數
    就在「左右腦年齡測試」熱傳不久後,便有網友稱,同樣的答案會得到不同的分數,並質疑測試結果的真實性。同時有程式設計師表示,研究測試代碼發現,測出來的年齡其實只是隨機數。相關專家認為,有關左右腦年齡的測試沒有科學依據,左右腦沒有分工,從事複雜活動時實為左右腦協同操作。左右腦年齡測試刷爆朋友圈「這個男人的眼睛在一條直線上嗎?我的結果是,左腦33歲,右腦5歲。」
  • 非專業程式設計師如何學好C語言,讓C語言成為你成功的最後一步?
    什麼是C 語言,怎麼學好C語言?C語言能做什麼?一想到那些還沒入坑,但是聽說計算機行業工資很高的朋友想要學習C語言。他們對很多的專業知識還是一知半解的,今天就給大家正式推薦一本書!另外還有很多這類的書籍供大家查閱,我和同學們都搶著看,C語言提升了不只是一點半點。
  • 程式設計師學院每周書單推薦
    從Java的基礎語法到最高級特性(深入的面向對象概念、多線程、自動項目構建、單元測試和調試等),本書都能逐步指導你輕鬆掌握。對C、C++以及Java語言都有獨到、深入的見解,以通俗易懂及小而直接的示例解釋了一個個晦澀抽象的概念。本書共22章,包含了Java語言基礎語法以及高級特性,適合各個層次的Java程式設計師閱讀,同時也是高等院校講授面向對象程序設計語言以及Java語言的絕佳教材和參考書。
  • c語言50本電子書
    C Primer Plus(第五版)中文版.pdfC專家編程.pdfC和C++內存管理.pdfC和指針.pdfC和指針(第二版).pdfC外掛編寫深究4.3.pdfC大綱[學習庫www.xuexi111.com].pdfC標準庫-中文.pdfC程式設計師面試100題.pptC經典算法大全
  • C語言程式設計師 必讀的5本書
    書籍是知識的豐富來源。你可以從書中學 到各種知識。書籍可以毫無歧視地向讀者傳達作者的本意。C語言是由 Dennis Ritchie在1969年到1973年在貝爾實驗室研發的。C語言可以把程序簡單地編譯為機器指令,使得它成為了最高效的語言。  為 什麼在程式設計師中,C語言如此流行呢?這背後有很多原因。首先,它獨立於平臺,可以運行在MAC、Linux、UNIX和PC各種環境中。
  • C語言程式設計師必讀的5本書
    C語言是由DennisRitchie在1969年到1973年在貝爾實驗室研發的。C語言可以把程序簡單地編譯為機器指令,使得它成為了最高效的語言。  為什麼在程式設計師中,C語言如此流行呢?這背後有很多原因。首先,它獨立於平臺,可以運行在MAC、Linux、UNIX和PC各種環境中。它是UNIX系統以及其他同樣結構的系統的默認語言。
  • 高級程式設計師考試經驗談
    高級程式設計師考試(以下簡稱高程考試)是計算機軟體資格與水平考試中難度比較大的一級,也是許多在校大學生熱衷參與的一項考試。下面是筆者參加高程考試的一點體會,希望對那些正在準備高程考試的朋友有所幫助。
  • 「左右腦年齡測試」不靠譜 程式設計師:結果實為隨機數
    測試結果引發質疑  「左右腦年齡測試」在朋友圈廣泛傳播後引發了網友熱議。做過測試題的王先生稱,根據結果,自己的右腦結果只有5歲,「可能是平時右腦用的比較少,以前從來沒想過智力跟實際年齡有這麼大差距」。但同時,王先生也提出了質疑,「自己做了很多次試驗,有幾次選了相同的答案,但是卻得到了不同的分數,而且結果也相差很大。」
  • 書籍分享|零基礎學習C語言(附電子書)
    它能為你系統學習c提供一個良好的平臺。作者:普拉達《The C programming language》拿到這本薄薄的書,很多人開始懷疑,C語言是這麼幾百頁能講清楚的麼。看完這本書,我想答案已經很明了,卻真的讓人感到震憾。什麼是好書?無法刪減的書才是真正的好書。
  • C語言學習推薦書籍
    第10章 文本處理第11章 分離用戶界面與內部實現第12章 撰寫設計文檔附錄C語言中接口定義的不同形式《華為技術有限公司c語言編程規範》有一句話說的很好,「規範大於配置」。如果你需要為一個團隊制定C編程規範,《華為技術有限公司c語言編程規範》可以作為你的參考。
  • 左右腦年齡測試火了!到底準不準?程式設計師這樣說...
    近日,關於「左右腦年齡測試 」 結果的圖在朋友圈引發熱傳。測試中,只要通過掃描測試結果圖中的二維碼並回答幾個設定的問題後,便會生成一張顯示有左右腦兩個年齡測試結果的圖片,圖片的下方還配有相關的文字解釋。有網友稱,同樣的答案會得到不同的分數,並質疑測試結果的真實性。同時有程式設計師表示,研究測試代碼發現,測出來的年齡其實只是隨機數。
  • 學C 語言者必看,C語言必過的經典總結.
    4)bit是位 是指為0 或者1。 byte 是指字節, 一個字節 = 八個位.5)一定要記住 二進位 如何劃成 十進位。 概念常考到的:1、編譯預處理不是C語言的一部分,不再運行時間。C語言編譯的程序稱為源程序,它以ASCII數值存放在文本文件中。2、每個C語言程序中main函數是有且只有一個。3、在函數中不可以再定義函數。
  • 初級程式設計師、中級程式設計師,高級程式設計師是如何定義的?
    >高級程式設計師能夠寫一些框架,甚至一個新語言在具體分析各個級別程式設計師的定義的時候,我們先來想一下,大部分的程式設計師來源於:學校、自學和培訓機構。從學校畢業的程式設計師,普通的大學生,不說那些研究生或者參加過一些學術研究和比賽的人。大部分的學生雖然學過很多,但是基本上能掌握一門語言都算是很好的。會做一些項目,但沒有具體的經驗累積。大多數的人連初級都算不上,因此只能實習或者是選擇培訓機構繼續深造;自學的程式設計師們,能夠學成的人一般都具有比較強的自制能力。
  • 程式設計師學習C語言編程的4種方法,C語言之父的大作不容錯過!
    不過,有些程式設計師是直接從C語言強勢入門編程的。那麼,如何學習C語言呢?下面w3cschool提供4種入門C語言的方法:0、刷題絕大多數的程式設計師學編程的時候,還是會開啟簡單粗暴的刷題模式。閱代碼百遍不如手過一遍,比如閱讀《C程序設計語言》,最好是將裡面的代碼和習題編譯並運行,甚至還需要調試和改進。有些代碼一眼看過去很簡單,不過當你敲一遍過去,會遇到很多細節的問題。普通程式設計師和優秀程式設計師的區別,往往也在對於細節的觀察和感知上面。
  • 程式設計師的人生必做100件事中,一定有讀這14本經典!
    「為自己的人生充電」這一項,一定是程式設計師們的「人生必做100」的NO.1!如果大家在平時多多閱讀教材,積累編程經驗,再多的人生必做事項,都會在不知不覺中被你完成!無論是初級程式設計師還是經驗豐富的開發者,這些影響了幾代程式設計師的經典圖書都是「充電學習」的必讀選項。
  • 2021年 Linux 上最佳 C/C++ IDE 和代碼編輯器
    回到本文的標題,讓我們討論這些程式語言的Linux IDE和代碼編輯器。但是在進入本節之前,首先讓我們了解這兩種程式語言的重要性,當然還要了解IDE和代碼編輯器的重要性。C語言通常被稱為所有主要程式語言之母。它是在1972年首次開發的,並且被許多人認為是第一種高級程式語言。除此之外,C語言是編程世界歷史上唯一存在時間最長的程式語言。此外,它也是大多數程式設計師開始他們旅程的語言之一。
  • 剖析C語言中a=a+++a的無聊問題
    這種純屬C語言 「二」 級的問題應該是從a+++a引申出來的吧。於是乎兄弟姐妹們開始討論它的運算結果,以及改如何理解。更有人寫出(a++)+(++a) a+(++(++a)) ((a++)++)+a這樣的東西,問應該如何計算。我表示鴨梨很大...