c語言數組的聲明和初始化_C聲明和初始化能力問題和解答

c語言數組的聲明和初始化

This section contains aptitude questions and answers on C language Declarations and Initialization.

本節包含有關C語言聲明和初始化的適切性問題和解答。

1) What will be the output of following program ?
int main(){
int m=10;
int x=printf("%d ",m);
printf("%d",x);
return 0;
}

  1. 10 3

  2. 103

  3. 10 2

  4. 102

Answer & Explanation

Correct answer: 1
10 3

printf() returns total number of printed characters, the statement int x=printf("%d ",m) will print 10 (10 and one space) and return 3. Thus output will be 10 3 [10<space>3].

1)以下程序的輸出是什么?
  1. 10 3

  2. 103

  3. 10 2

  4. 102

答案與解釋

正確答案:1
10 3

printf()返回已打印字符的總數,語句int x = printf(“%d”,m)將打印10 (10和一個空格)并返回3 。 因此輸出將為10 3 [10 <space> 3]

2) What will be the output of following program ?
int main(){
int x='A';
printf("%02X",x);
return 0;
}

  1. 65

  2. 97

  3. Error

  4. 41

Answer & Explanation

Correct answer: 4
41

Statement int x='A'; will declare x as integer and assign the ASCII value of 'A' (that is 65) in it. And the statement printf("%02X",x); will print the ASCII value (65) in the 2 bytes Hexadecimal forma (that is 41). Thus output will be 41.

2)以下程序的輸出是什么?
  1. 65

  2. 97

  3. 錯誤

  4. 41

答案與解釋

正確答案:4
41

語句int x ='A'; 將x聲明為整數,并在其中分配ASCII值“ A” (即65)。 和語句printf(“%02X”,x); 將以2個字節的十六進制格式(即41)打印ASCII值(65)。 因此輸出將為41

3) What will be the output of following program ?
int main(){
char a=0b1010;
printf("%02X",a);
return 0;
}

  1. 0A

  2. A

  3. 0a

  4. 10

Answer & Explanation

Correct answer: 1
0A

Statement char a = 0b1010; will declare 'a' as the character and assign the binary value (1010) in it as 0b1010 is assigned. 0b is an integer literal which simply defines the number afterward the prefix 0b to be in binary. Thus 0b1010 is actually binary value 1010 that is equivalent to 10 in decimal. And the statement printf("%02X", a); will print the value of a (10) in 2 bytes hexadecimal format, that is 0A. Thus, the output will be 0A.

3)以下程序的輸出是什么?
  1. 0A

  2. 一個

  3. 0a

  4. 10

答案與解釋

正確答案:1
0A

語句char a = 0b1010; 將聲明'a'作為字符,并在分配了0b1010時為其分配二進制值( 1010 )。 0b是整數文字,它簡單地將前綴0b之后的數字定義為二進制。 因此, 0b1010實際上是二進制值1010 ,它等于十進制的10 。 和語句printf(“%02X”,a); 將打印的在2(10)中的值的字節的十六進制格式,即0A。 因此,輸出將為0A 。

4) What will be the output of following program (on 32 bit compiler)?
int main(){
char a=2*2+2;
printf("%d",a);
return 0;
}

  1. 0

  2. Garbage

  3. 6

  4. 8

Answer & Explanation

Correct answer: 3
6

Statement char a = 2*2+2; will declare a as the character and assign 6 in it (according to operator precedence and associability, operator * (Multiply) is evaluated first, then the expression 2*2+2 will be evaluated as (2*2) +2). So a has an ASCII value of 6. Since the placeholder in the printf("%d", a); statement is %d, thus, it prints the ASCII value of the character type variable.

4)以下程序(在32位編譯器上)的輸出是什么?
  1. 0

  2. 垃圾

  3. 6

  4. 8

答案與解釋

正確答案:3
6

語句char a = 2 * 2 + 2; 將聲明a作為字符并在其中分配6(根據運算符優先級和可關聯性,運算符* (乘)將首先求值,然后表達式2 * 2 + 2將求值為(2 * 2)+2) 。 因此, a的ASCII值為6。由于printf(“%d”,a)中的占位符; 語句是%d ,因此,它輸出字符類型變量的ASCII值。

5) What will be the output of following program ?
int main(){
unsigned char x=-1;
printf("%02X",x);
return 0;
}

  1. 0xFFFFFFFF

  2. FF

  3. 0xFF

  4. ff

Answer & Explanation

Correct answer: 2
FF

Statement unsigned char x=-1; will declare x as unsigned char and assign value -1 in it, which is of type int. When we assign -1 in an unsigned type of variable, it converts this value from int to unsigned int. The rule for signed-to-unsigned conversion is reduced modulo (UINT_MAX + 1, so -1 will be converted to UINT_MAX, where UINT_MAX represents the highest (maximum) value of the UNIT type of variable.

UNIT_MAX% (UNIT_MAX+1) = -1 || UNIT_MAX

unsigned char can store 2 bytes of value that is equivalent to 255 in decimal and FF in Hexadecimal, thus output will be FF since we are printing up to two hexadecimal places.

5)以下程序的輸出是什么?
  1. 0xFFFFFFFF

  2. FF

  3. 0xFF

  4. ff

答案與解釋

正確答案:2
FF

語句unsigned char x = -1; 會將x聲明為unsigned char并在其中分配值-1 ,其類型為int 。 當我們以無符號類型的變量分配-1時,它將把此值從int轉換為unsigned int 。 有符號到無符號轉換的規則以模為模( UINT_MAX + 1 ,因此-1將被轉換為UINT_MAX ,其中UINT_MAX表示UNIT類型變量的最高(最大值)值。

UNIT_MAX%(UNIT_MAX + 1)= -1 || UNIT_MAX

unsigned char可以存儲2個字節的值,這些值等于十進制的255和十六進制的FF ,因此由于我們最多打印兩個十六進制位置,因此輸出將為FF 。

6) Which is the correct name of a variable?

6)變量的正確名稱是什么?

(A) -var (B) var-1 (C) _var (D) var_1

(A) -var (B) var-1 (C) _var (D) var_1

  1. Only (A)

    僅(A)

  2. Only (B)

    僅(B)

  3. Both (A) and (B)

    (A)和(B)

  4. Both (C) and (D)

    (C)和(D)

Answer & Explanation 答案與解釋

Correct answer: 4
Both (C) and (D)

正確答案:4
(C)和(D)

Read: Identifier/Variable naming conventions in C language [Rules and Recommendations].

閱讀: C語言中的標識符/變量命名約定[規則和建議]。

7) Which special character can be used to separate two parts (words) of a variable/identifier name?

7)哪個特殊字符可用于分隔變量/標識符名稱的兩個部分(單詞)?

  1. - (Hyphen)

    -(連字號)

  2. _ (Underscore)

    _(下劃線)

  3. $ (Dollar)

    $(美元)

  4. # (Hash)

    #(哈希)

Answer & Explanation 答案與解釋

Correct answer: 2
_ (Underscore)

正確答案:2
_(下劃線)

Underscore ( _ ) is the only special character that can be used within the variable/identifiers name, it can be placed as first character, between the words and as the last character.

下劃線(_)是可在變量/標識符名稱中使用的唯一特殊字符,可以將其作為第一個字符,單詞之間和最后一個字符放置。

8) What will be the result of following program?

8)后續程序會有什么結果?

#include <stdio.h>
int main()
{
char x='AB';	
printf("%c",x);	
return 0;
}

  1. Error

    錯誤

  2. Runs successfully and prints 'A' (without quote)

    成功運行并顯示“ A”(不帶引號)

  3. Run with warnings and prints 'B' (without quote)

    運行警告并顯示“ B”(不帶引號)

  4. None of these

    都不是

Answer & Explanation 答案與解釋

Correct answer: 3
Run with warnings and prints 'B' (without quote)

正確答案:3
運行警告并顯示“ B”(不帶引號)

This program will run with a warning, because we are trying to initialize character variable with multi character constant, overflow will be occurred and output will be 'B'.

該程序將運行警告,因為我們正在嘗試使用多字符常量初始化字符變量,將發生溢出并且輸出將為'B'

Warnings

警告事項

main.c:4:9: warning: multi-character character constant [-Wmultichar] 
char x='AB';
^
main.c:4:2: warning: overflow in implicit constant conversion [-Woverflow]
char x='AB';
^

9) Will following string be terminated with NULL?

9)后面的字符串會以NULL終止嗎?

char str[]={'H','e','l','l','o'};
  1. YES

  2. NO

    沒有

Answer & Explanation 答案與解釋

Correct answer: 2
NO

正確答案:2
沒有

No, such kind of initialization with declaration does not insert NULL at the end of the string.

不,這種帶有聲明的初始化不會在字符串末尾插入NULL。

Following two methods can be used to declare a string variable with NULL termination:

可以使用以下兩種方法聲明以NULL終止的字符串變量:

char str[]={'H','e','l','l','o','\0'};
char str[]="Hello";

Consider the following program to understand string initialization better:

考慮以下程序以更好地了解字符串初始化:

#include <stdio.h>
int main()
{
char str1[]={'H','e','l','l','o'};
char str2[]={'H','e','l','l','o','\0'};
char str3[]="Hello";
printf("%s\n",str1);
printf("%s\n",str2);
printf("%s\n",str3);
return 0;
}

Output

輸出量

Hello
Hello 
Hello

Here, str1 is not terminated with NULL and other string variables str2 and str3 are terminated with NULL.

在這里 , str1不以NULL終止,而其他字符串變量str2和str3則以NULL終止。

10) Predict the output of following program.

10)預測以下程序的輸出。

#include <stdio.h>
int main()
{
int (x)=10;
printf("x= %d",x);
return 0;
}

  1. x= 10

    x = 10

  2. x= 0

    x = 0

  3. Error

    錯誤

  4. None of these

    都不是

Answer & Explanation 答案與解釋

Correct answer: 1
x= 10

正確答案:1
x = 10

Such kind of declaration form int (x)=10; is also acceptable in C/C++ programming language, compiler will not return any error.

這種聲明形式int(x)= 10; 在C / C ++編程語言中也是可以接受的,編譯器不會返回任何錯誤。

11) Predict the output of following program.

11)預測以下程序的輸出。

#include <stdio.h>
int main()
{
int i=50;
const int x=i++;
printf("x= %d",++x);
return 0;
}

  1. x= 50

    x = 50

  2. x= 51

    x = 51

  3. x= 52

    x = 52

  4. Error

    錯誤

Answer & Explanation 答案與解釋

Correct answer: 4
Error

正確答案:4
錯誤

Here, x is a read only (integer constant) and we cannot change the value of any constant, following error will occur:

在這里 , x是只讀的(整數常量),我們不能更改任何常量的值,將發生以下錯誤:

error: increment of read-only variable 'x'
printf("x= %d",++x);
^

12) Predict the output of following program.

12)預測以下程序的輸出。

#include <stdio.h>
int main()
{
int a=(10,20);
printf("a= %d",a);
return 0;
}

  1. a= 10

    a = 10

  2. a= 20

    a = 20

  3. a= 30

    a = 30

  4. Error

    錯誤

Answer & Explanation 答案與解釋

Correct answer: 2
a= 20

正確答案:2
a = 20

In the declaration statement int a=(10,20); value 10, 20 are placed within the brackets ( ), thus (10,20) will be evaluated first. Comma operator has left to right associativity, then result will be (20), thus output of the program will be x= 20.

在聲明語句中int a =(10,20); 值10、20放在方括號()中,因此將首先評估(10,20) 。 逗號運算符具有從左到右的關聯性,則結果將為(20) ,因此程序的輸出將為x = 20

翻譯自: https://www.includehelp.com/c/declaration-and-initialization-aptitude-questions-and-answers.aspx

c語言數組的聲明和初始化

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542675.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542675.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542675.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python2和python3的默認編碼_python2和python3哪個版本新

Python2 還是 Python3 &#xff1f; py2.7是2.x系列的最后一個版本&#xff0c;已經停止開發&#xff0c;不再增加新功能。2020年終止支持。 所有的最新的標準庫的更新改進&#xff0c;只會在3.x的版本里出現。Python3.0在2008年就發布出來&#xff0c;而2.7作為2.X的最終版本并…

html-css樣式表

一、CSS&#xff1a;Cascading Style Sheet—層疊樣式表&#xff0c;其作用是美化HTML網頁。 樣式表分類&#xff1a;內聯樣式表、內嵌樣式表、外部樣式表 1、內聯樣式表 和HTML聯合顯示&#xff0c;控制精確&#xff0c;但是可重用性差&#xff0c;冗余多。 例如&#xff1a;&…

java 棧 先進后出_棧先進后出,堆先進先出

1.棧(stack)與堆(heap)都是Java用來在Ram中存放數據的地方。與C不同&#xff0c;Java自動管理棧和堆&#xff0c;程序員不能直接地設置棧或堆。2.棧的優勢是&#xff0c;存取速度比堆要快&#xff0c;僅次于直接位于CPU中的寄存器。但缺點是&#xff0c;存在棧中的數據大小與生…

c#給定二維數組按升序排序_在數組中按升序對數字進行排序| 8086微處理器

c#給定二維數組按升序排序Problem: Write a program in 8086 microprocessor to sort numbers in ascending order in an array of n numbers, where size n is stored at memory address 2000 : 500 and the numbers are stored from memory address 2000 : 501. 問題&#xf…

使用python套用excel模板_Python自動化辦公Excel-從表中批量復制粘貼數據到新表

1、模塊安裝 1&#xff09;cmd模式下&#xff1a; pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlrd pip install -i https://pypi.tuna.tsinghua.edu.cn/simple openpyxl 2&#xff09;如果有安裝Pycharm&#xff0c;則在程序中操作如下&#xff1a; 菜單欄&…

在HubSpot是如何應對Fat JAR困境的

在七月底&#xff0c;Spring Boot和Dropwizard分別發布了1.4和1.0版本&#xff0c;它們都是基于Fat JAR的。隨著人們更多地采用這些框架和微服務架構&#xff0c;Fat JAR成為了通用的部署機制。\\Fat JAR技術會將Java應用的所有依賴打包到一個bundle之中&#xff0c;便于執行&a…

給定數字的b+樹創建_在C ++中找到給定數字中的兩個的下一個和上一個冪

給定數字的b樹創建Problem statement: 問題陳述&#xff1a; Find Next and previous power of two of a given number 查找給定數字中兩個的下一個和上一個冪 Next power of two 下一個二的冪 Example(1):input: 22output: 32 ( as 32 is 2^5)Example(2):input: 54output…

java 字節數組作用_這段java代碼中字節數組b起到了什么作用?

importjava.io.*;importjavax.swing.*;publicclassIOMonitor{publicstaticvoidmain(String[]temp){//TODO自動生成的方法存根byteb[]newbyte[2];try{FileInputStreamfisnewFileInput...import java.io.*;import javax.swing.*;public class IOMonitor {public static void main…

如何查看本地的崩潰log_過年回家,還怕搶不到票?程序員教你如何搶票

2019年接近尾聲&#xff0c;距離春節回家的日子越來越近&#xff0c;26日起&#xff0c;2020年除夕火車票正式開售&#xff0c;搶票大戰也進入白熱化階段。是否為某搶票 App 加速而煩惱&#xff0c;是否為車票“秒光而煩惱”。別慌&#xff0c;作為連“對象”都是 new 出來的程…

獲取列表中包含的元素數 在C#中

Given a list, and we have to count its total number of elements using List.Count property. 給定一個列表&#xff0c;我們必須使用List.Count屬性計算其元素總數 。 C&#xff03;清單 (C# List) A list is used to represent the list of the objects, it is represent…

I00037 虧數(Deficient number)

數論中&#xff0c;若一個正整數除了本身之外所有因子之和比此數自身小&#xff0c;則稱此數為虧數。虧數&#xff08;Deficient number&#xff09;也稱為缺數&#xff0c;參見百度百科_虧數&#xff0c;或參見維基百科的Deficient number。虧數在OEIS中的數列號為A005100。 問…

hashmap轉紅黑樹的閾值為8_面試必考的 HashMap,這篇總結到位了

點擊藍色“JavaKeeper”關注我喲加個“星標”&#xff0c;一起成長&#xff0c;做牛逼閃閃的技術人1 概述HashMap是基于哈希表實現的,每一個元素是一個key-value對,其內部通過單鏈表解決沖突問題,容量不足(超過了閥值)時,同樣會自動增長.HashMap是非線程安全的,只適用于單線程環…

linux用戶組管理命令_Linux用戶和組命令能力問題和解答

linux用戶組管理命令This section contains Aptitude Questions and Answers on Linux User and Group Commands. 本節包含有關Linux用戶和組命令的 Aptitude問答。 1) Which of the following commands is used to create a new user in the Linux operating system? create…

Failed to start firewalld.service: Unit firewalld.service is masked.

2019獨角獸企業重金招聘Python工程師標準>>> FireWall in Centos 7 masked How to resolve the error message belowFailed to issue method call: Unit firewalld.service is masked. The main reason a service is masked is to prevent accidental starting or e…

mysql第二個索引_MySQL高級第二章——索引優化分析

一、SQL性能下降原因1.等待時間長&#xff1f;執行時間長&#xff1f;可能原因&#xff1a;查詢語句寫的不行索引失效(單值索引、復合索引)CREATE INDEX index_user_name ON user(name);(底層做了一個排序)CREATE INDEX index_user_nameEmail ON user(name,email);查詢關聯join…

遞歸反轉鏈表改變原鏈表嗎_在不使用遞歸的情況下找到鏈表的長度

遞歸反轉鏈表改變原鏈表嗎Solution: 解&#xff1a; Algorithm to find length 查找長度的算法 Input: 輸入&#xff1a; A singly linked list whose address of the first node is stored in a pointer, say head. 一個單鏈表 &#xff0c;其第一個節點的地址存儲在指針(例…

西瓜仿站高手v1.08官方正式版

2019獨角獸企業重金招聘Python工程師標準>>> 西瓜仿站高手是一款綠色好用的由追風網絡出品的網站模板批量下載軟件&#xff0c;西瓜仿站高手是一款仿站工具&#xff0c;仿站神器。軟件功能強大&#xff0c;能夠幫你輕松幫你下載任意網站、任意模板&#xff0c;并且速…

用hundred造句子_八個有趣的開學破冰游戲,線上線下都能用

知道大家最近都很忙&#xff0c;所以省略開篇&#xff0c;直接上正題——開學“破冰游戲”走起&#xff01;一、你比劃我來猜把詞語展示在PPT上&#xff0c;猜詞的同學背對PPT&#xff0c;其他同學可以看到詞語并且用身體動作把詞語表現出來&#xff0c;直到猜詞的同學可以把詞…

java 執行順序_Java代碼執行順序

程序中代碼執行的順序非常重要&#xff0c;稍有不慎便會是程序運行出錯&#xff0c;那么我將結合實例來分析代碼中的執行。名詞解釋首先了解幾個名詞&#xff1a;非靜態代碼塊直接由 { } 包起來的代碼&#xff0c;稱為非靜態代碼塊靜態代碼塊直接由 static { } 包起來的代碼&am…

mysql 包含的那些文件

*.frm是描述了表的結構 *.MYD保存了表的數據記錄 *.MYI則是表的索引 ibd是MySQL數據文件、索引文件&#xff0c;無法直接讀取。 轉載于:https://www.cnblogs.com/07byte/p/5823667.html