C and Objective-c test questions explained (The)

Source: Internet
Author: User
Tags signal handler

Test Address: http://www.eosgarden.com/en/articles/objc-quizz/take/

This is a few days ago friends shared obj-c test questions, a total of 57 questions. Self-Mastery OC I went to do the next test, the result has been a more serious psychological blow, test center is very fine, some even very low ground. Prepare to explain these topics in 2 sessions, each of which is explained below. Some of the topics of the author himself have some questions, welcome to explore.

1.What is ' Nil ' in objective-c? What is "Nil" in OC?

Answer: (void *) 0

Description: ' null ', ' nil ', and ' nil ' are null pointers to 0 addresses. ' Nil ' and ' nil ' are defined in OC as "darwin_null", i.e. (void *) 0

2.What will happen while the following program is executed? What happens after the following code runs?

[CPP]View Plaincopy
    1. #include <stdlib.h>
    2. int main ( void)
    3. {
    4. char * ptr = NULL;
    5. Free (PTR);
    6. return 0;
    7. }


Answer: No problems will arise.

Description: The C standard library defines the free () null pointer as safe.

3.What method is called by the NSLog function when the '%@ ' sequence is present? What method is called "NSLog" when calling%@ methods?

Answer: Description

Description: OC Basics

4.Which is the correct syntax to declare a function pointer name "foo" returning an integer and have an integer as Argu ment?

Declares a function pointer named "foo" that returns an integral type and accepts an integer parameter

Answer: Int (* foo) (int)

Description: The underlying function pointer problem

5.How Many bytes is used to store a "long long" data type?long long type is a few bytes?

Answer: Implementation defined is defined based on the implementation of (compiler)

Note: The author was wrong at the time, the author chose 8 bytes, although the majority of compilers on a long long is indeed 8 bytes, but this argument is debatable. In fact, the C standard does not specifically specify the number of bytes that the basic type should be, and this is also related to the machine, OS, compiler.

6.What is the difference between the "unsigned int" and the "Nsuinteger" data types? " What is the difference between unsigned int "and" Nsinteger "?

Answer: It depends on the processor. Depending on the processor

Description: The basic problem, read the definition of Nsuinteger should know that 32-bit and 64-bit definitions are different. Defined as follows:

[CPP]View Plaincopy
    1. #if __lp64__ | | (target_os_embedded &&!) Target_os_iphone) | | Target_os_win32 | | Ns_build_32_like_64
    2. typedef Long Nsinteger;
    3. typedef unsigned long Nsuinteger;
    4. #else
    5. typedef INT Nsinteger;
    6. typedef unsigned int nsuinteger;
    7. #endif

7.What would be printed the output from the following program?

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. typedef Union
  3. {
  4. Short s;
  5. Char C;
  6. }
  7. Sc
  8. int main ( void)
  9. {
  10. SC u;
  11. U.S. = 0x602;
  12. printf ( "%d\n", u.c);
  13. return 0;
  14. }

Answer: Machine dependant. Different results are different depending on the machines.

Description: Involves the size of the problem, and referring to the 5th question, the short byte length is also variable.

8.What can assume when calling the "Stringwithstring:" Method on a "NSString" object?

When using NSString's stringwithstring, we can learn:

Answer: The returned object is auto-released. The returned NSString object is auto-released (self-releasing).

Description: Basic

9.Is it possible to has multiple instances of the "NSAutoreleasePool" class in the same program?

Can I use multiple nsautoreleasepool in one program?

Answer: Obviously, yes.

Description: Multithreaded programming is especially useful.

10.Which line of the following code would be reported as a error by the compiler?

Which line of code does the following compiler error?

[CPP]View Plaincopy
  1. int main ( void)
  2. {
  3. Const char * s = "Hello world!"; / * Line 3 * /
  4. s = "Hello universe!"; / * Line 4 * /
  5. s[0] = ' A '; / * Line 5 * /
  6. return 0;
  7. }

Answer: Line Fifth

Description: Some people may not know the meaning of the const char * type. Read from right to left, read * as pointer to, that is, a pointer to const char, pointer to the const char, so the pointer is mutable, and the pointed char is immutable.

11.Which is true about the following statement, placed outside of any function or method?

What is the correct statement for the following statements that are placed outside any method?

[CPP]View Plaincopy
    1. static int foo;

Answer: The variable cannot is accessed directly from other files. The variable has a default initial value 0. Variables cannot be accessed directly from other files, and the variable defaults to 0.

Description: The answer to this question is as above, but I am skeptical of the answers. You cannot access this statement directly from another file, declare it in the header file, and you can access it whenever you import the header file.

12.Is garbage collection available on IPhone OS?

Does iOS have a garbage collection mechanism?

Answer: No

Description: Base. Be aware, however, that Mac OS has a garbage collection mechanism.

13.What can say about the "memory addresses that would be" printed by the following program?

What do you think of the memory address printed out by the following code?

[CPP]View Plaincopy
  1. #import <Cocoa/Cocoa.h>
  2. int main ( void)
  3. {
  4. NSAutoreleasePool * POOL;
  5. NSString * S1;
  6. NSString * S2;
  7. NSString * S3;
  8. NSString * S4;
  9. Pool = [[NSAutoreleasePool alloc] init];
  10. S1 = @"Hello world!";
  11. S2 = @"Hello world!";
  12. s3 = [NSString stringwithstring: @"Hello world!"];
  13. s4 = [[NSString alloc] initwithstring: @"Hello world!"];
  14. printf ( "%p\n%p\n%p\n%p\n", S1, S2, S3, S4);
  15. [S4 release];
  16. [Pool release];
  17. return 0;
  18. }

Answer: All addresses'll be the same. All addresses are the same
Description: The result of an immutable string that the compiler automatically optimizes.

14.Which line can is used to compile an objective-c executable named "Test" from a "test.m" file?

Which of the following commands can compile TEST.M as an executable file?

Answer: gcc-wall-framework cocoa-o test TEST.M

Explanation: This question is not very clear, because usually not very will go to compile manually, but happened to be blindfolded.

15.Does the Objective-c compiler treats the identifiers of an enumeration as Integer constants?

Does an OC compiler handle an enumeration type as an integral type?

Answer: Obviously yes

Description: Basic

16.What would be printed by the following program? The output of the following programs?

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. int main ( void)
  3. {
  4. unsigned int array[2 [2] = {{0, 1}, {2, 3}};
  5. unsigned int i = 0;
  6. unsigned int sum = 0;
  7. int x = 0;
  8. int y = 0;
  9. For (i = 0; i < 4; ++i)
  10. {
  11. x = i% 2;
  12. y = (x)? 0:1;
  13. Sum + = array[x [y];
  14. }
  15. printf ( "%d\n", sum);
  16. return 0;
  17. }

Answer: 6

Description: Basic

17.What happen when a floating point value is assigned to an integer variable?

What happens when a floating-point data is assigned to an integer variable?

Answer: Rounding, floating-point reduction

Description: Basic

18.In theory, is it safe-to-call a function of the standard C library from a signal handler?

In theory, is it safe to call standard library functions in a signal processing function?

Answer: Not Safe

Description: Functions from the C standard Library May is not reentrant.

Because a static or global variable may be used in a standard library function (which can be manipulated concurrently by both the main and signal processing functions), this is a non-reentrant function, so it is unsafe to call a standard library function in a signal processing function. Refer to the relevant section of Apue for details.

PS: This question to thank Unix Daniel @delo in the micro-blog, the answer, too professional. UNIX-related knowledge we still have some shortcomings.

19.What would be printed by the following program?

What are the output results of the following programs?

[CPP]View Plaincopy
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. int main ( void)
  4. {
  5. int * PTR;
  6. int i;
  7. PTR = ( int *) malloc (4 * sizeof ( int));
  8. For (i = 0; i < 4; i++)
  9. {
  10. ptr[i] = i;
  11. }
  12. printf ( "%d,", *ptr++);
  13. printf ( "%d,", + + (*ptr));
  14. printf ( "%d,", *++ptr);
  15. printf ( "%d\n", ++*ptr);
  16. return 0;
  17. }

Answer: 0,2,2,3

Description: 1: Print ptr[0], pointer right shift, 2:ptr[1] self-increment, print ptr[1];3: pointer right, print ptr[2];4:ptr[2] self-increment, print ptr[2]

Are it possible to use the dynamic libraries when developing for IPhone OS?

Can I use the dynamic link library in iOS development?

Answer: No

Description: iOS development can only use static libraries

21.Which of the following creates a class that conforms to a protocol?

How do I inherit the agreement?

Answer: @interface ClassName < ProtocolName >

Description: Basic

22.What is the default visibility for instance variables?

What is the default access permission for instance variables?

Answer: @protected

Description: Similar to other languages, although it may not be used in iOS.

23.Is it possible to use C + + when developing for IPhone OS?

Can I use C + + in iOS development?

Answer: Yes

Note:. mm files can be mixed, and the cocos2d box2d physics engine is a common C + + writing.

24.What can say about the code below? Yuan Fang, what do you think?

[CPP]View Plaincopy
    1. -(void) foo: (NSString *) s
    2. {
    3. s = @"Hello world!";
    4. }

Answer: Monseigneur, this code is absolutely no problem

Description: Although the code can run, be careful that the method does not change the value of the externally passed-in string, the relationship between the formal parameter and the argument.

25.Consider the following code:

[CPP]View Plaincopy
    1. Double x = 5/10-2/2 * 4;

Please write the value of x below:

Answer:-4

Description: Super Basics, note divisible

26.After the execution of the following code, what is the retain count of the "S1" and "S2" objects?

What is the reference count for S1 and S2 after executing the following code?

[CPP]View Plaincopy
    1. Nsmutablestring * S1;
    2. nsmutablestring * S2;
    3. S1 = [[[Nsmutablestring alloc] initwithstring:@"Hello world!"] autorelease];
    4. [[[[S1 retain] retain] retain] release];
    5. s2 = [S1 copy];
    6. [S1 release];

Answer: S1:2,s2:1

Description: S1:alloc+1,retain three times +3,release two times-2, a reference count of 2;s2:copy will produce a new object, +1, a reference count of 1.

27.What can say about the code below? Yuan Fang, what do you think?

[CPP]View Plaincopy
    1. [Foo bar:1];
    2. [Foo bar:1 Y:2];

Answer: A different method is called.

Description: The section before the colon in OC is considered part of the method name, such as the first method named bar:, the second method is named Bar:y:, a completely different two method, not an overload.

28.Is It true that the "initialize" message was sent to a class before any other messages, even class methods calls?

The "Initialize" (initialize) message is not the first message to be sent to the class, even earlier than invoking the class method?

Answer: Yes

Description: This problem actually I was not quite sure, consulted the relevant official documents, the official documents on the NSObject class method initialize has the following description:

Initialize

Initializes the receiver before it ' s used (before it receives its first message).

+ (void) initializediscussion

The runtime sends initialize to each class-a program exactly one time just before the class, or any class that inherits from It, is sent their first message from within the program. (Thus the method never be invoked if the class was not used.) The runtime sends the initialize message to classes in a thread-safe manner. Superclasses receive this message before their subclasses.

The main idea: the runtime sends only one "Initailize" to each class or its inheriting class, which is the first message to be sent within a program. (Therefore, "initialize" is not called if the class is not used.) The runtime sends a "INITAILIZE" message to the class in a thread-safe manner. The parent class receives the message before the child class.

29.Is there a difference between these, statements?

Are the following 2-paragraph declaration statements different?

[CPP]View Plaincopy
    1. Const char * FOO;
    2. char * Const bar;

Answer: Yes, the meaning is different

Description: Refer to the 10th method, the first punctuation as a pointer to the const char, the second punctuation as a const pointer to char, a pointer to the character constant variable, and a pointer to the characters variable constant, meaning completely different.

30.In theory, which function is faster: "strcpy" or "memcpy"?

Theoretically, which of the "strcpy" and "memcpy" methods is more efficient?

Answer: memcpy

Description: strcpy more than memcpy a search string end character "" "operation, slower than memcpy.

Original link: http://blog.csdn.net/xiemotongye/article/details/8915039

C and Objective-c test questions explained (The)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.