C and Objective-c test questions explained (next)

Source: Internet
Author: User



After the day before the blog, today continue to explain the 27 questions. Test Question address: http://www.eosgarden.com/en/articles/objc-quizz/take/



31.Which of the following can be inherited?



Which of the following can be inherited?



Answer: Protocols,classes. Protocols and classes



Note: Be aware that categories cannot be inherited.






32.How Do you throw an exception?



How do I throw an exception?



Answer: @throw E



Description: The exception handling notation for the Object-c language is similar to C + + and Java. Plus with Nsexception,nserror or custom classes, you can add a powerful error handling mechanism to your application. The exception handling mechanism is supported by this four keyword: @try, @catch, @thorw, @finally.






33.What would be printed by the following program?



What are the output results of the following programs?





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


Answer: Undefined behavior not clear






Description: The C specification says that changing the value of a same variable multiple times in the same statement results in an Undefined behavior. The C language specification says that the same variable is changed multiple times in the same statement, and the result is ambiguous. The author initially thought is 7, but Xcode run after found the result is 8, indeed very wonderful.






34.What would be printed by the following program?



What are the output results of the following programs?





#include <stdio.h>
int main( void )
{
    printf( "%u", -1 );
    return 0;
}


Answer: the largest int value.






Description: Base, involving the complement of negative numbers.






35.What class specifiers is supported by objective-c?



What is the class descriptor supported in OC?



Answer: OC does not support class specifiers.



Description: Just not supported.






36.Which is true about the "realloc" function?



The following is the correct way to say "realloc"?





void * ReAlloc (void * ptr, size_t size);


Answer: It changes the size of the allocated memory pointed to by the first argument. Changes the allocated memory size that is pointed by the initial parameter.
The newly allocated memory will be uninitialized. The newly allocated RAM is uninitialized.



A NULL pointer can be passed safely as first argument. The null pointer acts as a realloc parameter.



Description: The role of realloc is to reallocate memory, first determine whether the current pointer has enough contiguous space, if there is, expand mem_address point to the address, and will mem_address return, if the space is not enough, first according to the size of the newsize assigned space, Copies the original data from the beginning to the newly allocated memory area, then releases the original mem_address memory area, and returns the first address of the newly allocated memory area. That is, the address of the memory block is redistributed. Additionally, the newly allocated memory is uninitialized, and the null pointer can be used as its first parameter.









37.When was Objective-c 2.0 released by Apple?



OC2.0, when did it come out?



Answer: 2007



Description: This question really does not have any meaning, we measured is the technology, is not the history.






38.Is it absolutely necessary for a objective-c class to has "NSObject" as root class?



Is it necessary for classes in OC to use NSObject as the base class?
Answer: no need



Note: It is not necessary to take nsobject as the base class, but it should be noted that "Init", "Alloc" and other inherited from the NSObject method can not be used.






39.When using the "Removeobjectatindex:" Method of the "Nsmutablearray" class, is the retain count of the removed object C Hanged?



When using Nsmutablearray's Removeobjectatindex: method, does the reference count of the object being moved change?



Answer: Yes.



Description: Used to know that Nsmutablearray and nsmutabledictionary in the object when the reference count of +1, moved out-1.






40.What can say about the code below, assuming we is developing for IPhone OS?



Suppose we develop iOS programs, what do you think of the following code?





- (void)bar
{
    NSArray * array = [NSArray arrayWithObjects: @"Hello", @"world", nil];
}
- (void)foo
{
    [NSThread detachNewThreadSelector:@selector(bar) toTarget:self withObject:nil];
}


Answer: Runtime error run-time errors






Description: The standard answer is a run-time error, the author of this slightly doubt. This code does look like there is a memory leak, the object constructed using the class construction method will be autorelease (self-release), and this method runs in the child thread, the iOS program has no garbage collection mechanism, in multi-threaded state, the child thread must have a autorelease Pool (auto-free pools) to manage Autorelease objects. But this code should not cause a run-time error, pro-Test, Xcode4.6.2,ipad Simulator 6.1, the runtime did not error. However, it is important to remind that there is memory leak code is not advisable, although there may be no problem, but wait for the problem when you want to locate the issue is very time-consuming and laborious.






41.What is the correct syntax for declaring the prototype of a block named "foo" returning an integer and taking a charact Er pointer as argument?



The following declares a name of "Foo", returns an integral type, and takes a character pointer as the correct block syntax for the parameter?



Answer: Int (^ foo) (char *);



Description: In fact, you can see that the declaration syntax of block is quite similar to the declaration syntax of a function pointer, and the power of block is that it can modify variables under the same scope.






42.What would be printed by the following program?



Output from the following programs?





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


Answer: 1020






Description: Base, shift left 2 bit is multiplied by 4,255x4=1020.






What type of variable does you need to implement the singleton pattern?



When you want to use singleton mode, the variable should define why type?



Answer: Static statically type



Note: Whether lazy or a hungry man, as long as you use a singleton, you should make sure that there is only one instance of this class, and static statically typed is a must.






What would be being printed by the following program?



Output from the following programs?





#include <string.h>
#include <stdio.h>
char * get_ptr( void );
char * get_ptr( void )
{
    static char ptr[ 10 ] = "123456789";
    return ptr;
}
int main( void )
{
    char * ptr = "00000";
    strcpy( get_ptr() + 4, ptr );
    ptr = get_ptr();
    5[ strcpy( ptr, "12345" ) ] = ‘6‘;
    printf( "%s\n", get_ptr() );
    return 0;
}


Answer: 123456000






Description: See "5[strcpy (PTR," 12345 ")] = ' 6 ';" This line is really stunned, generally do not write, but this writing does not make an error.






45.What is a category?



What is category (category) in OC?



Answer: a means of adding a method to an existing class.



Note: Be aware that category cannot add variables to existing classes, such as adding a variable, or inheriting it.






46.What would be printed by the following program?



Output from the following programs?





#include <stdio.h>
void foo( char ** s );
void foo( char ** s )
{
    ++s;
}
int main( void )
{
    char * s = "Hello World!";
    foo( &s );
    foo( &s );
    printf( "%s\n", s );
    return 0;
}


Answer: Hello world!






Description: Problems with formal parameters and actual parameters






47.Does the retain count of an object change when do you send a autorelease message to it?



When you send a autorelease message to an object, does its reference count change?



Answer: No



Note: When the autorelease message is sent, the object is added to the Autorelease pool (auto-free), and the reference count is not changed. When the pool drain is automatically released, the object receives the relase message, and the reference count is reduced by one.






48.What would be printed by the following program, assuming it would run on a architecture with 8 bytes pointers?



The output of the following program? Suppose we are running on a 8-byte pointer to the schema.





#include <stdio.h>
int main( void )
{
    const char foo[] = "Hello";
    const char * bar = " Universe!";
    printf( "%lu\n", sizeof( foo ) + sizeof( bar ) );
    return 0;
}


Answer: 14






Description: Foo is an array, sizeof (foo) is 6 (note the end of string "" "), bar is the pointer, sizeof (BAR) is 8, 6 + 8 = 14. When sizeof, be aware of the difference between the array and the pointer.



49.What does objective-c not support?



Which of the following options is OC not supported?



Answer: Class variables variables, private methods proprietary method, Protected methods protection method.



Description: Unlike C + +, OC does not support class static member variables (that is, classes variables are not supported), and it is common practice to define static variables outside of class instead. In addition, the methods in OC have no public-private points, and are all public.






50.What is the C type behind the objective-c ' id ' type, used to represent objects?



What type of "id" in OC is essentially c?



Answer: Structure structural body



Description: This problem involves the lower level, the ID type is defined in the Objc.h file, the code is as follows:





typedef struct objc_class *Class;
typedef struct objc_object {
    Class isa;
} *id;





What would be being printed by the following program?






Output from the following programs?





#include <stdio.h>
int main( void )
{
    int a    = 31;
    int b    = 10;
    int c    = 3;
    double d =  a / b % c;
    printf( "%f\n", d );
    return 0;
}


Answer: 0






Description: Exactly, it should be 0.000000.






52.In The following code, what's the value returned by the last statement?



What is the return value of the last statement in the following code?





#define M( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
#define N( x, y, z ) ( M( x, M( y, z ) ) )
N( 1, 2, 3 )


Answer: 1






Description: Base, note that the use of parentheses should be noted when define macros are defined.






53.Which of the following is false?



What's wrong with the following statement?



The answer: The called object is available as an automatic variable (as a, or super) when a method is called.
Methods in static libraries must is present at link time.



Description: Really do not understand the meaning of these two words. I don't know the right way to say it?






54.What happens if categories define methods with the same names for the same class?



What happens when two categories (categories) Define a method of the same name for the same class?



Answer: Undefined Behavior not sure



Description: The runtime will randomly select, remember, do not try this code easily! Will prevent you from locating the bug.






Following code, what is the value of ' Y ' after the last statement?



What is the Y value of the last statement in the following code?





#define ADD( x ) x + 1
int x = 5;
int y = 10;
y = ADD( x ) * 2;


Answer: 7






Description: Base, define is just a simple replacement, define the use of parentheses in the definition to prevent the result from being inconsistent with expectations.






56.What happen when a exception is thrown @synchronized block?



What happens when an exception is thrown in a @synchronized block?



Answer: Object Unlocked



Description: The role of @synchronized block is generally multi-threaded programming when the object is locked, to achieve the purpose of mutual exclusion, there is an exception thrown when the object is unlocked.






57.Is there a memory leak in the following code?



Does the following code have a memory leak?





@interface Foo: NSObject
{
    id object;
}
@property( retain ) __strong id object;
- ( id )initWithObject: ( id )object;
@end
@implementation Foo
@synthesize object;
- ( void )dealloc
{
    [ object release ];
    [ super dealloc ];
}
- ( id )initWithObject: ( id )o
{
    if( ( self = [ self init ] ) )
    {
        self.object = [ o retain ];
    }
    return self;
}
@end


Answer: There is a memory leak






Note: The Retain property is set at the time of @property, then the set method called automatically generated when the object is assigned is retain once, so the retain of object o in Initwithobject is superfluous, which causes the reference count to be greater than 0. Memory is not released, causing memory leaks



PS: Thanks again for the @im_xiaobin that provided the test address, as well as for the valuable suggestions @delo @ The great floret, and the people who can take the time to read my technical blog!!!



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



C and Objective-c test questions explained (next)


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.